diff --git a/application/config/database.php b/application/config/database.php index e8ab51e..de9475a 100644 --- a/application/config/database.php +++ b/application/config/database.php @@ -76,8 +76,8 @@ $query_builder = TRUE; $db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', - 'username' => 'jostheta', - 'password' => 'Pa$$w0rd', + 'username' => 'root', + 'password' => '', 'database' => 'gforms', 'dbdriver' => 'mysqli', 'dbprefix' => '', diff --git a/application/config/routes.php b/application/config/routes.php index 46c544d..b9589e7 100644 --- a/application/config/routes.php +++ b/application/config/routes.php @@ -9,6 +9,7 @@ $route['my_drafts'] = 'Forms/my_drafts/$1'; $route['forms/delete/(:num)'] = 'forms/delete_form/$1'; $route['forms/respond/(:num)'] = 'forms/respond/$1'; $route['responses'] = 'Forms/list_user_forms'; +$route['edit_form/(:num)'] = 'Forms/edit_form/$1'; diff --git a/application/controllers/Forms.php b/application/controllers/Forms.php index 3191d94..a493d4d 100644 --- a/application/controllers/Forms.php +++ b/application/controllers/Forms.php @@ -242,15 +242,39 @@ class Forms extends CI_Controller $this->load->view('templates/footer'); } - public function edit_form(){ + public function edit_form($form_id) { + $this->load->model('Form_model'); + $formData = $this->input->post('formData'); $decodedData = json_decode($formData, true); - // Process the form data here - // Example: Save the form data to the database - $this->load->model('Form_model'); + if (!$decodedData) { + log_message('error', 'Failed to decode form data: ' . $formData); + echo json_encode(['status' => 'error', 'message' => 'Invalid form data']); + return; + } + + log_message('debug', 'Decoded form data: ' . print_r($decodedData, true)); + + $form_data = array( + 'title' => $decodedData['title'], + 'description' => $decodedData['description'], + ); + + try { + $this->Form_model->update_form($form_id, $form_data); + $this->Form_model->delete_for_edit($form_id); + $this->Form_model->save_for_edit($decodedData, $form_id); + + echo json_encode(['status' => 'success', 'message' => 'Form data updated successfully.']); + } catch (Exception $e) { + log_message('error', 'Exception occurred: ' . $e->getMessage()); + echo json_encode(['status' => 'error', 'message' => 'An error occurred while updating the form data.']); + } } - + + + } diff --git a/application/models/Form_model.php b/application/models/Form_model.php index cf9b812..1fd85d9 100644 --- a/application/models/Form_model.php +++ b/application/models/Form_model.php @@ -184,7 +184,64 @@ class Form_model extends CI_Model { $query = $this->db->get('forms'); return $query->result(); } + + public function delete_for_edit($form_id) { + // Fetch question IDs + $question_ids = $this->db->select('question_id') + ->from('questions') + ->where('form_id', $form_id) + ->get() + ->result_array(); + + // Extract question IDs to a simple array + $question_ids = array_column($question_ids, 'question_id'); + + // Delete existing options for the questions of this form + if (!empty($question_ids)) { + $this->db->where_in('question_id', $question_ids); + $this->db->delete('options'); + } + + // Delete existing questions for the form + $this->db->where('form_id', $form_id); + $this->db->delete('questions'); + } + + public function save_for_edit($formData, $form_id) { + $user_id = $this->session->userdata('user_id'); + + // Log the formData being processed + log_message('debug', 'Saving for form_id: ' . $form_id . ', formData: ' . print_r($formData, true)); + + foreach ($formData['questions'] as $question) { + $this->db->insert('questions', [ + 'form_id' => $form_id, + 'question_text' => $question['question_text'], + 'question_type' => $question['question_type'] + ]); + $question_id = $this->db->insert_id(); + + // Log the insert question SQL + log_message('debug', 'Insert question SQL: ' . $this->db->last_query()); + + if ($question['question_type'] !== 'paragraph') { + foreach ($question['options'] as $option) { + $this->db->insert('options', [ + 'question_id' => $question_id, + 'option_text' => $option['option_text'] + ]); + + // Log the insert option SQL + log_message('debug', 'Insert option SQL: ' . $this->db->last_query()); + } + } + } + } + + - } + + + diff --git a/assets/js/edit.js b/assets/js/edit.js index baaf2a8..3b79e7b 100644 --- a/assets/js/edit.js +++ b/assets/js/edit.js @@ -117,6 +117,7 @@ $(document).ready(function() { }).trigger('change'); $('#update-form').click(function() { + var form_id = $(this).data('form_id'); var formData = { title: $('#form-title').val(), description: $('#form-desc').val(), @@ -143,10 +144,13 @@ $(document).ready(function() { formData.questions.push(questionData); }); - console.log(formData); + console.log('Form Data:', formData); + + var url = base_url + 'edit_form/' + form_id; + console.log('AJAX URL:', url); $.ajax({ - url: base_url + 'forms/edit_form', + url: url, type: 'POST', data: { formData: JSON.stringify(formData) }, success: function(response) { @@ -155,7 +159,11 @@ $(document).ready(function() { }, error: function(xhr, status, error) { console.error('Error updating form data:', error); + console.log('XHR:', xhr); + console.log('Status:', status); } }); }); + + });