**********MODEL****************
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;
}
}
}
********************Controller**********************
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);
}
}
***********UPDATED HTML*************************
### Updated HTML
```html
Google Form Clone
*************************************88*************************
new Controller
load->model('Form_model'); // Load your Form_model
}
public function save() {
// Handle AJAX post data
$form_data = $this->input->post('form_data'); // Assuming your AJAX post sends 'form_data'
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')
);
$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
}
});
}