From f944a7637cadf354dbbc3e70c10a2598d83fbc0b Mon Sep 17 00:00:00 2001 From: torun23 Date: Fri, 12 Jul 2024 17:20:41 +0530 Subject: [PATCH] commit --- application/config/routes.php | 2 + application/controllers/Form.php | 22 + application/controllers/Home.php | 8 +- application/controllers/New_form.php | 32 ++ application/controllers/Users.php | 18 +- application/controllers/Your_controller.php | 20 + application/models/Create_model.php | 24 + application/models/Form_model.php | 40 ++ application/models/Your_model.php | 19 + application/views/templates/form_title.php | 19 + application/views/templates/forms2.txt | 584 ++++++++------------ application/views/templates/forms_ui.php | 16 +- application/views/templates/header.php | 2 +- assets/css/styles.css | 15 +- assets/js/scripts.js | 50 +- 15 files changed, 493 insertions(+), 378 deletions(-) create mode 100644 application/controllers/Form.php create mode 100644 application/controllers/New_form.php create mode 100644 application/controllers/Your_controller.php create mode 100644 application/models/Create_model.php create mode 100644 application/models/Form_model.php create mode 100644 application/models/Your_model.php create mode 100644 application/views/templates/form_title.php diff --git a/application/config/routes.php b/application/config/routes.php index 3b91ad3..1e47882 100644 --- a/application/config/routes.php +++ b/application/config/routes.php @@ -7,5 +7,7 @@ $route['default_controller'] = 'home/index2'; $route['404_override'] = ''; $route['translate_uri_dashes'] = FALSE; $route['start'] = 'home/index'; +$route['new_form'] = 'home/create_form'; +$route['title_desc'] = 'home/title'; diff --git a/application/controllers/Form.php b/application/controllers/Form.php new file mode 100644 index 0000000..1b70743 --- /dev/null +++ b/application/controllers/Form.php @@ -0,0 +1,22 @@ +load->model('Form_model'); + } + + public function submit() { + $form_data = json_decode($this->input->raw_input_stream, true); + + if ($this->Form_model->save_form($form_data)) { + $response = array('status' => 'success', 'message' => 'Form submitted successfully.'); + } else { + $response = array('status' => 'error', 'message' => 'Error submitting form.'); + } + + echo json_encode($response); + } +} \ No newline at end of file diff --git a/application/controllers/Home.php b/application/controllers/Home.php index bc84a1f..d2d129a 100644 --- a/application/controllers/Home.php +++ b/application/controllers/Home.php @@ -34,7 +34,13 @@ class Home extends CI_Controller $this->load->view('templates/footer'); } -public function create_form(){ +public function design_form(){ $this->load->view('templates/forms_ui'); } +public function title(){ + $this->load->view('templates/header'); + $this->load->view('templates/form_title'); + $this->load->view('templates/footer'); + +} } \ No newline at end of file diff --git a/application/controllers/New_form.php b/application/controllers/New_form.php new file mode 100644 index 0000000..c4e0cb3 --- /dev/null +++ b/application/controllers/New_form.php @@ -0,0 +1,32 @@ +form_validation->set_rules('title', 'Title', 'required'); + $this->form_validation->set_rules('description', 'Description', 'required'); + + + if ($this->form_validation->run() === FALSE) { + $this->load->view('templates/header'); + $this->load->view('templates/form_title',$data); + $this->load->view('templates/footer'); + } else { + // $enc_password = md5($this->input->post('password')); + $this->load->model('create_model'); + // $user_id = $this->session->userdata('user_id'); + + + $this->create_model->details(); + // $this->user_model->register(); + + + // $this->session->set_flashdata('user_registered', 'You are now registered and can log in'); + redirect('home/design_form'); + } + + } +} + +?> \ No newline at end of file diff --git a/application/controllers/Users.php b/application/controllers/Users.php index 8571e81..fbb0e0d 100644 --- a/application/controllers/Users.php +++ b/application/controllers/Users.php @@ -27,8 +27,14 @@ class Users extends CI_Controller } } - //login user + +/** + * function to login into google forms + * @param null + * @return mixed(data return type) + * @author torun + */ public function login() { $data['title'] = 'Sign In'; @@ -48,22 +54,24 @@ class Users extends CI_Controller $password = md5($this->input->post('password')); $this->load->model('user_model'); // Login user - $user_id = $this->user_model->login($username, $password); + $person_id = $this->user_model->login($username, $password); - if ($user_id) { + if ($person_id) { // Create session $user_data = array( - 'user_id' => $user_id, + 'user_id' => $person_id, 'username' => $username, 'logged_in' => true ); $this->session->set_userdata($user_data); + $person_id = $this->session->userdata('user_id'); + // Set message $this->session->set_flashdata('user_loggedin', 'You are now logged in'); - redirect('start'); + redirect('title_desc'); } else { // Set message $this->session->set_flashdata('login_failed', 'Login is invalid'); diff --git a/application/controllers/Your_controller.php b/application/controllers/Your_controller.php new file mode 100644 index 0000000..4700dd6 --- /dev/null +++ b/application/controllers/Your_controller.php @@ -0,0 +1,20 @@ +session->userdata('user_id'); + + // Query to fetch form_id from database based on user_id + $form_id = $this->your_model->getFormIdByUserId($user_id); // Replace with your actual model method + + // Return form_id as JSON response + $this->output + ->set_content_type('application/json') + ->set_output(json_encode(array('form_id' => $form_id))); +} +} diff --git a/application/models/Create_model.php b/application/models/Create_model.php new file mode 100644 index 0000000..9b8723f --- /dev/null +++ b/application/models/Create_model.php @@ -0,0 +1,24 @@ + +session->userdata('user_id'); + + // Prepare data to insert into forms table + $data = array( + 'user_id' => $user_id, + 'title' => $this->input->post('title'), + 'description' => $this->input->post('description') + ); + + // Insert data into forms table + $this->db->insert('forms', $data); + } +} + +?> diff --git a/application/models/Form_model.php b/application/models/Form_model.php new file mode 100644 index 0000000..c3c4e5a --- /dev/null +++ b/application/models/Form_model.php @@ -0,0 +1,40 @@ +db->trans_start(); + + foreach ($form_data as $section) { + $question_data = array( + 'form_id' => $section['form_id'], + 'text' => $section['text'], + 'type' => $section['type'], + 'required' => $section['required'], + 'created_at' => date('Y-m-d H:i:s') + ); + + $this->db->insert('questions', $question_data); + $question_id = $this->db->insert_id(); + + foreach ($section['options'] as $option_text) { + $option_data = array( + 'question_id' => $question_id, + 'text' => $option_text, + 'created_at' => date('Y-m-d H:i:s') + ); + + $this->db->insert('options', $option_data); + } + } + + $this->db->trans_complete(); + + if ($this->db->trans_status() === FALSE) { + return false; + } else { + return true; + } + } +} \ No newline at end of file diff --git a/application/models/Your_model.php b/application/models/Your_model.php new file mode 100644 index 0000000..39bf107 --- /dev/null +++ b/application/models/Your_model.php @@ -0,0 +1,19 @@ + +db->select('form_id'); + $this->db->where('user_id', $user_id); + $query = $this->db->get('forms'); // Replace 'your_forms_table' with your actual table name + + if ($query->num_rows() > 0) { + $row = $query->row(); + return $row->form_id; + } else { + return null; // Handle case when form_id is not found + } +} +} \ No newline at end of file diff --git a/application/views/templates/form_title.php b/application/views/templates/form_title.php new file mode 100644 index 0000000..af75e00 --- /dev/null +++ b/application/views/templates/form_title.php @@ -0,0 +1,19 @@ + +
+
+

+ +

+
+ + +
+
+ + +
+ +
+
+ + diff --git a/application/views/templates/forms2.txt b/application/views/templates/forms2.txt index badaaed..f6c5c0c 100644 --- a/application/views/templates/forms2.txt +++ b/application/views/templates/forms2.txt @@ -1,280 +1,46 @@ -To achieve the functionality where the form data is submitted and stored in the database using CodeIgniter, we need to set up the necessary controllers, models, and views. Below is a detailed guide on how to implement this. +**********MODEL**************** -### Step 1: Update JavaScript (scripts.js) +db->trans_start(); - // Function to add an option based on type - function addOption(type, container) { - let optionIndex = container.children().length + 1; - let optionHtml; - if (type === 'multiple-choice' || type === 'checkboxes') { - optionHtml = ` -
- - - × -
- `; - } else if (type === 'dropdown') { - optionHtml = ` -
- - × -
- `; + foreach ($form_data as $section) { + $question_data = array( + 'form_id' => $section['form_id'], + 'text' => $section['text'], + 'type' => $section['type'], + 'required' => $section['required'], + 'created_at' => date('Y-m-d H:i:s') + ); + + $this->db->insert('questions', $question_data); + $question_id = $this->db->insert_id(); + + foreach ($section['options'] as $option_text) { + $option_data = array( + 'question_id' => $question_id, + 'text' => $option_text, + 'created_at' => date('Y-m-d H:i:s') + ); + + $this->db->insert('options', $option_data); + } } - container.append(optionHtml); - } - // Function to create a new form section - function createFormSection() { - let newSection = ` -
-
- ${index === 1 ? '
' : ''} - - - - -
-
-
- `; - $('#form-container').append(newSection); - index++; - positionAddSectionButton(); - } + $this->db->trans_complete(); - // Position the "Add Section" button dynamically - function positionAddSectionButton() { - if (activeSection) { - let position = activeSection.position(); - let buttonWidth = $('#add-section-btn').outerWidth(); - let buttonHeight = $('#add-section-btn').outerHeight(); - - $('#add-section-btn').css({ - position: 'absolute', - left: position.left - buttonWidth - 47 + 'px', - top: position.top + activeSection.height() / 2 - buttonHeight / 2 + 'px' - }); - } - } - - // Event listener for "Add Section" button - $('#add-section-btn').on('click', function() { - createFormSection(); - $('.form-section').removeClass('active'); - activeSection = $('.form-section').last(); - activeSection.addClass('active'); - positionAddSectionButton(); - }); - - // Event listener for changing question type - $(document).on('change', '.custom-select', function() { - let type = $(this).val(); - let container = $(this).closest('.form-section').find('.options-container'); - container.empty(); - - $(this).closest('.form-section').find('.add-option-btn').remove(); - - if (type === 'short-answer') { - container.append(''); - } else if (type === 'paragraph') { - container.append(''); + if ($this->db->trans_status() === FALSE) { + return false; } else { - addOption(type, container); - $(this).closest('.form-section').append(''); + return true; } - }); - - // Event listener for adding an option - $(document).on('click', '.add-option-btn', function() { - let type = $(this).closest('.form-section').find('.custom-select').val(); - let container = $(this).closest('.form-section').find('.options-container'); - addOption(type, container); - }); - - // Event listener for deleting a section - $(document).on('click', '.delete-section-icon', function() { - let section = $(this).closest('.form-section'); - let prevSection = section.prev('.form-section'); - let nextSection = section.next('.form-section'); - section.remove(); - if (section.hasClass('active')) { - activeSection = null; - } - if (prevSection.length > 0) { - prevSection.find('.delete-section-icon').appendTo(prevSection.find('.form-section')); - activeSection = prevSection; - } else if (nextSection.length > 0) { - nextSection.find('.delete-section-icon').appendTo(nextSection.find('.form-header')); - activeSection = nextSection; - } - positionAddSectionButton(); - }); - - // Event listener for deleting an option - $(document).on('click', '.delete-option-icon', function() { - $(this).closest('.option').remove(); - }); - - // Event listener for required toggle button - $(document).on('click', '.required-toggle', function() { - $(this).closest('.form-section').toggleClass('required'); - }); - - // Event listener for preview button - $('#preview-btn').on('click', function() { - let previewWindow = window.open('', '_blank'); - let previewContent = ` - - - Form Preview - - - - -
-
-

Untitled Form

-
- `; - $('.form-section').each(function() { - previewContent += '
'; - previewContent += '
'; - previewContent += ''; - previewContent += '
'; - let type = $(this).find('.custom-select').val(); - let optionsContainer = $(this).find('.options-container'); - - if (type === 'multiple-choice') { - optionsContainer.find('.option').each(function() { - previewContent += ` -
- - -
- `; - }); - } else if (type === 'checkboxes') { - optionsContainer.find('.option').each(function() { - previewContent += ` -
- - -
- `; - }); - } else if (type === 'short-answer') { - previewContent += ''; - } else if (type === 'paragraph') { - previewContent += ''; - } else if (type === 'dropdown') { - let dropdownHtml = ''; - previewContent += dropdownHtml; - } - previewContent += '
'; - }); - previewContent += ` - -
- - - `; - previewWindow.document.write(previewContent); - previewWindow.document.close(); - }); - - // Event listener for clicking on a form section - - - $(document).on('click', '.form-section', function() { - $('.form-section').removeClass('active'); - $(this).addClass('active'); - activeSection = $(this); - positionAddSectionButton(); - }); - - // Submit button click event to save data to the database - $(document).on('click', '#submit-btn', function() { - let formData = []; - - // Iterate through each form section - $('.form-section').each(function() { - let sectionData = { - question: $(this).find('.untitled-question').val(), - type: $(this).find('.custom-select').val(), - required: $(this).find('.required-toggle').prop('checked') ? 1 : 0, - options: [] - }; - - // Iterate through options if any - $(this).find('.option').each(function() { - sectionData.options.push($(this).find('.option-label').val()); - }); - - formData.push(sectionData); - }); - - // Ajax call to submit data - $.ajax({ - url: "", - type: "POST", - data: { - formData: formData - }, - success: function(response) { - alert(response); // Handle response from server - } - }); - }); - - // Initialize sortable feature for form sections - $('#form-container').sortable({ - placeholder: 'ui-state-highlight', - start: function (event, ui) { - ui.placeholder.height(ui.item.height()); - }, - stop: function (event, ui) { - positionAddSectionButton(); - } - }); - - $('#form-container').disableSelection(); -}); -``` - -### Step 2: Create Controllers and Models in CodeIgniter - -#### 1. Controller: Form.php - -```php + } +} +********************Controller********************** load->model('Form_model'); } - public function index() { - // Load view for form creation - $this->load->view('create_form'); - } + public function submit() { + $form_data = json_decode($this->input->raw_input_stream, true); - public function submit_form_data() { - $formData = $this->input->post('formData'); - - // Example to get user ID (you should have this implemented in your authentication flow) - $userId = 1; // Replace with your actual user ID retrieval logic - - // Insert form data into database using model - foreach ($formData as $section) { - $formId = $this->Form_model->insert_form($userId, $section['question']); - - // Insert questions into database - $this->Form_model->insert_question($formId, $section['type'], $section['required']); - - // Insert options into database if any - if (!empty($section['options'])) { - foreach ($section['options'] as $option) { - $this->Form_model->insert_option($formId, $option); - } - } + if ($this->Form_model->save_form($form_data)) { + $response = array('status' => 'success', 'message' => 'Form submitted successfully.'); + } else { + $response = array('status' => 'error', 'message' => 'Error submitting form.'); } - echo "Form data submitted successfully!"; + echo json_encode($response); } } -?> -``` - -#### 2. Model: Form_model.php - -```php - $userId, - 'description' => $description, - 'created_at' => date('Y-m-d H:i:s') - ); - $this->db->insert('forms', $data); - return $this->db->insert_id(); - } - - public function insert_question($formId, $type, $required) { - // Insert question data - $data = array( - 'form_id' => $formId, - 'type' => $type, - 'required' => $required, - 'created_at' => date('Y-m-d H:i:s') - ); - $this->db->insert('questions', $data); - } - - public function insert_option($formId, $text) { - // Insert option data - $data = array( - 'question_id' => $formId, // Assuming form ID here, adjust as per your structure - 'text' => $text - ); - $this->db->insert('options', $data); - } - -} -?> -``` - -### Step 3: Create Views - -#### 1. create_form.php (View for form creation) - +***********UPDATED HTML************************* +### Updated HTML ```html + @@ -387,7 +89,7 @@ class Form_model extends CI_Model { font-size: 18px; } - .navbar-custom .navbar-nav > li > a { + .navbar-custom .navbar-nav>li>a { color: white; font-size: 16px; } @@ -399,8 +101,8 @@ class Form_model extends CI_Model { } + - -
@@ -424,35 +125,196 @@ class Form_model extends CI_Model {
- + + +
- + + -``` -### Step 4: Database Structure -Ensure your database tables (`users`, `forms`, `questions`, `options`) are set up according to your described structure: -- `users` table: id (primary key), username, email, password, created_at -- `forms` table: id (primary key), user_id (foreign key to users.id), description, created_at -- `questions` table: id (primary key), form_id (foreign key to forms.id), text, type, required, created_at -- `options` table: id (primary key), question_id (foreign key to questions.id), text +*************************************88************************* +new Controller +load->model('Form_model'); // Load your Form_model + } -### Step 6: Testing + public function save() { + // Handle AJAX post data + $form_data = $this->input->post('form_data'); // Assuming your AJAX post sends 'form_data' -- Access the form creation page (`http://yourdomain.com/form`) and create a form. -- Click on the "Submit" button to save the form data. -- Verify that the data is correctly saved in your database tables. + if (!empty($form_data)) { + foreach ($form_data as $section) { + $question_data = array( + 'form_id' => $section['form_id'], + 'text' => $section['text'], + 'type' => $section['type'], + 'required' => $section['required'], + 'created_at' => date('Y-m-d H:i:s') + ); -This setup assumes you have basic knowledge of CodeIgniter and have configured your environment correctly. Adjust paths (`base_url()`) and database configurations as per your setup. \ No newline at end of file + $question_id = $this->Form_model->save_question($question_data); + + // Save options for this question + foreach ($section['options'] as $option_text) { + $option_data = array( + 'question_id' => $question_id, + 'text' => $option_text, + 'created_at' => date('Y-m-d H:i:s') + ); + + $this->Form_model->save_option($option_data); + } + } + + echo json_encode(array('success' => true)); + } else { + echo json_encode(array('success' => false)); + } + } + +} +******************************newMODEL********** +db->insert('questions', $question_data); + return $this->db->insert_id(); + } + + public function save_option($option_data) { + $this->db->insert('options', $option_data); + } + +} +******************JAVASCRIPT************updatead +function collectFormData() { + let formData = []; + let formId = $('#form-id').val(); + + $('.form-section').each(function() { + let questionText = $(this).find('.untitled-question').val(); + let type = $(this).find('.custom-select').val(); + let required = $(this).find('.required-toggle').is(':checked'); + let options = []; + + $(this).find('.option-label').each(function() { + options.push($(this).val()); + }); + + formData.push({ + form_id: formId, + text: questionText, + type: type, + required: required, + options: options + }); + }); + + $.ajax({ + url: 'Questions/save', // Endpoint to handle saving questions + type: 'POST', + dataType: 'json', + data: { form_data: formData }, + success: function(response) { + if (response.success) { + alert('Form data saved successfully!'); + // Handle success actions + } else { + alert('Failed to save form data.'); + // Handle failure actions + } + }, + error: function(xhr, status, error) { + alert('Error: ' + error); + // Handle error actions + } + }); +} diff --git a/application/views/templates/forms_ui.php b/application/views/templates/forms_ui.php index 95c4a13..a696c7c 100644 --- a/application/views/templates/forms_ui.php +++ b/application/views/templates/forms_ui.php @@ -18,19 +18,18 @@ .navbar-custom .navbar-brand { color: white; - font-size: 18px; /* Adjust font size for navbar links */ + font-size: 18px; } - .navbar-custom .navbar-nav > li > a { + .navbar-custom .navbar-nav>li>a { color: white; - font-size: 16px; /* Adjust font size for navbar links */ + font-size: 16px; } - /* Additional styling for submit button */ #submit-btn { margin-top: 20px; - float: left; /* Align button to the left */ - clear: both; /* Clear float to ensure proper layout */ + float: left; + clear: both; } @@ -59,14 +58,15 @@
- - + + + diff --git a/application/views/templates/header.php b/application/views/templates/header.php index 5aa2869..bf05ff8 100644 --- a/application/views/templates/header.php +++ b/application/views/templates/header.php @@ -29,7 +29,7 @@
  • Register
  • session->userdata('logged_in')): ?> -
  • Create Form
  • +
  • Create Form
  • Logout
  • diff --git a/assets/css/styles.css b/assets/css/styles.css index 4ddd236..f8c908b 100644 --- a/assets/css/styles.css +++ b/assets/css/styles.css @@ -7,7 +7,20 @@ body { position: relative; margin-top: 30px; } - +.form_container_top{ + box-sizing: border-box; + margin-top: 10px; + font-family: Arial, Helvetica, sans-serif; + font-size: 13px; + font-weight: 400; + line-height: 100%; + width: 100%; + border: none; + outline: none; + border-bottom: 1px solid #f4f4f9; + color: black; + height: 20px; + } /* Form header styles */ .form-header { background-color: white; diff --git a/assets/js/scripts.js b/assets/js/scripts.js index 12d4d7e..50335f7 100644 --- a/assets/js/scripts.js +++ b/assets/js/scripts.js @@ -201,7 +201,7 @@ K }); previewContent += ''; }); previewContent += ` - + @@ -210,6 +210,7 @@ K }); previewWindow.document.close(); }); + // Activate the section;repositions add section button $(document).on('click', '.form-section', function() { $('.form-section').removeClass('active'); @@ -228,5 +229,52 @@ K }); } }); + $('#submit-btn').on('click', function() { + let formData = collectFormData(); + + $.ajax({ + url: 'form/submit', + type: 'POST', + data: JSON.stringify(formData), + contentType: 'application/json; charset=utf-8', + dataType: 'json', + success: function(response) { + alert('Form submitted successfully!'); + console.log(response); + }, + error: function(error) { + alert('Error submitting form!'); + console.log(error); + } + }); + }); + + function collectFormData() { + let formData = []; + + // let formId = $('#form-id').val(); + + $('.form-section').each(function() { + let questionText = $(this).find('.untitled-question').val(); + let type = $(this).find('.custom-select').val(); + let required = $(this).find('.required-toggle').is(':checked'); + let options = []; + + $(this).find('.option-label').each(function() { + options.push($(this).val()); + }); + + formData.push({ + // form_id: formId, + text: questionText, + type: type, + required: required, + options: options + }); + }); + + return formData; + } + $('#form-container').disableSelection(); }); \ No newline at end of file