edit is functional

This commit is contained in:
jostheta 2024-07-17 00:50:43 +05:30
parent 77b8770aef
commit 31efaa0ac6
5 changed files with 100 additions and 10 deletions

View File

@ -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' => '',

View File

@ -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';

View File

@ -242,17 +242,41 @@ 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.']);
}
}
}

View File

@ -185,6 +185,63 @@ class Form_model extends CI_Model {
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());
}
}
}
}
}

View File

@ -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);
}
});
});
});