This commit is contained in:
torun23 2024-07-15 18:15:25 +05:30
parent f944a7637c
commit 21213a9f21
7 changed files with 100 additions and 78 deletions

View File

@ -0,0 +1,24 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class New_form_controller extends CI_Controller {
public function submit_form() {
// no need to decode the data
$formData = $this->input->post('formData');
$formId = $this->session->userdata('form_id');
if ($formId) {
// Save questions and options associated with the form_id
$this->load->model('new_form_model');
$this->new_form_model->save_form_data($formId, $formData);
echo json_encode(['status' => 'success', 'message' => 'Form data submitted successfully']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Failed to submit form data']);
}
}
}
?>

View File

@ -1,20 +0,0 @@
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Your_controller extends CI_Controller
{
public function getFormId()
{
// Check if user is logged in and get user_id from session
$user_id = $this->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)));
}
}

View File

@ -1,11 +1,8 @@
<?php <?php
defined('BASEPATH') or exit('No direct script access allowed'); class Create_model extends CI_Model {
class Create_model extends CI_Model public function details() {
{
public function details()
{
// Retrieve user_id from session // Retrieve user_id from session
$user_id = $this->session->userdata('user_id'); $user_id = $this->session->userdata('user_id');
@ -18,7 +15,13 @@ class Create_model extends CI_Model
// Insert data into forms table // Insert data into forms table
$this->db->insert('forms', $data); $this->db->insert('forms', $data);
}
}
// Store form_id in session
$formId = $this->db->insert_id();
$this->session->set_userdata('form_id', $formId);
}
}
?> ?>

View File

@ -0,0 +1,34 @@
<?php
class New_form_model extends CI_Model {
public function save_form_data($formId,$formData) {
if (!$formId) {
return false; // Handle error if formId is not valid
}
foreach ($formData['questions'] as $question) {
$questionData = [
'form_id' => $formId,
'text' => $question['text'],
'type' => $question['type'],
'required' => ($question['required'] == 'true') ? 0 : 1
];
$this->db->insert('questions', $questionData);
$questionId = $this->db->insert_id(); // Get the inserted question_id
foreach ($question['options'] as $option) {
$optionData = [ 'question_id' => $questionId,
'option_text' => $option];
// Insert option into options table
$this->db->insert('options', $optionData);
}
}
return true; // Return true indicating success
}
}
?>

View File

@ -1,19 +0,0 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Your_model extends CI_Model {
public function getFormIdByUserId($user_id)
{
$this->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
}
}
}

View File

@ -35,6 +35,9 @@
</head> </head>
<body> <body>
<script>
var base_url = '<?php echo base_url(); ?>';
</script>
<nav class="navbar navbar-inverse navbar-custom"> <nav class="navbar navbar-inverse navbar-custom">
<div class="container"> <div class="container">
<div class="navbar-header"> <div class="navbar-header">

View File

@ -228,16 +228,39 @@ K });
positionAddSectionButton(); positionAddSectionButton();
} }
}); });
function collectFormData() {
var formData = {
questions:[]
};
$('.form-section').each(function() {
var questionData = {
text : $(this).find('.untitled-question').val(),
type : $(this).find('.custom-select').val(),
required :$(this).find('.required-toggle').is(':checked'),
options:[]
};
$(this).find('.option-label').each(function() {
questionData.options.push($(this).val());
});
formData.questions.push(questionData);
});
console.log(formData);
return formData;
}
$('#submit-btn').on('click', function() { $('#submit-btn').on('click', function() {
let formData = collectFormData(); let formData = collectFormData();
console.log(formData);
$.ajax({ $.ajax({
url: '<?php echo base_url(); ?>form/submit', url: base_url + 'New_form_controller/submit_form',
type: 'POST', type: 'POST',
data: JSON.stringify(formData), data: {formData:formData},
contentType: 'application/json; charset=utf-8', dataType: 'JSON',
dataType: 'json',
success: function(response) { success: function(response) {
alert('Form submitted successfully!'); alert('Form submitted successfully!');
console.log(response); console.log(response);
@ -248,33 +271,7 @@ K });
} }
}); });
}); });
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(); $('#form-container').disableSelection();
}); });