updated to db

This commit is contained in:
josephkurian 2024-07-12 15:25:29 +05:30
parent b0c708f170
commit 5ce1802fa7
7 changed files with 36 additions and 20 deletions

View File

@ -7,7 +7,7 @@ class Forms extends CI_Controller
if(!$this->session->userdata('logged_in')){ if(!$this->session->userdata('logged_in')){
redirect('users/login'); redirect('users/login');
} }
$data['title'] = 'Create Post'; $data['title'] = 'Create Form';
$this->load->view('templates/header'); $this->load->view('templates/header');
$this->load->view('forms/create', $data); $this->load->view('forms/create', $data);
$this->load->view('templates/footer'); $this->load->view('templates/footer');
@ -21,6 +21,7 @@ class Forms extends CI_Controller
// Example: Save the form data to the database // Example: Save the form data to the database
$this->load->model('Form_model'); $this->load->model('Form_model');
$this->Form_model->save_form_data($decodedData); $this->Form_model->save_form_data($decodedData);
echo json_encode(['status' => 'success', 'message' => 'Form data submitted successfully']); echo json_encode(['status' => 'success', 'message' => 'Form data submitted successfully']);

View File

@ -1,10 +1,15 @@
<?php
class Form_model extends CI_Model { class Form_model extends CI_Model {
public function save_form_data($formData) { public function save_form_data($formData) {
$user_id = $this->session->userdata('user_id');
// Save the form data to the database // Save the form data to the database
$this->db->insert('forms', [ $this->db->insert('forms', [
'title' => $formData['title'], 'title' => $formData['title'],
'description' => $formData['description'] 'description' => $formData['description'],
'user_id' => $user_id
]); ]);
$formId = $this->db->insert_id(); $formId = $this->db->insert_id();
@ -15,7 +20,6 @@ public function save_form_data($formData) {
'question_text' => $question['question'], 'question_text' => $question['question'],
'question_type' => $question['type'] 'question_type' => $question['type']
]); ]);
$questionId = $this->db->insert_id(); $questionId = $this->db->insert_id();
if ($question['type'] !== 'paragraph') { if ($question['type'] !== 'paragraph') {

View File

@ -1,5 +1,5 @@
<?php <?php
//Here Users is the table from the database //Here users is the table from the database
class User_model extends CI_Model{ class User_model extends CI_Model{
public function register($enc_password){ public function register($enc_password){
@ -12,7 +12,7 @@
); );
// Insert user // Insert user
return $this->db->insert('Users', $data); return $this->db->insert('users', $data);
} }
// Log user in // Log user in
@ -21,7 +21,7 @@
$this->db->where('username', $username); $this->db->where('username', $username);
$this->db->where('password', $password); $this->db->where('password', $password);
$result = $this->db->get('Users'); $result = $this->db->get('users');
if($result->num_rows() == 1){ if($result->num_rows() == 1){
return $result->row(0)->user_id; return $result->row(0)->user_id;
@ -32,7 +32,7 @@
// Check username exists // Check username exists
public function check_username_exists($username){ public function check_username_exists($username){
$query = $this->db->get_where('Users', array('username' => $username)); $query = $this->db->get_where('users', array('username' => $username));
if(empty($query->row_array())){ if(empty($query->row_array())){
return true; return true;
} else { } else {
@ -42,7 +42,7 @@
// Check email exists // Check email exists
public function check_email_exists($email){ public function check_email_exists($email){
$query = $this->db->get_where('Users', array('email' => $email)); $query = $this->db->get_where('users', array('email' => $email));
if(empty($query->row_array())){ if(empty($query->row_array())){
return true; return true;
} else { } else {

View File

@ -10,7 +10,7 @@
<br> <br>
<!-- New Questions will get added here --> <!-- New Questions will get added here -->
<div class="question-box" id="question-template" style="display:none;"> <div class="question-box" id="question-template" style="display:none;" data-question-type="multiple-choice">
<!-- This is the question-box header contains question, type, add an img --> <!-- This is the question-box header contains question, type, add an img -->
<div class="question-box_header"> <div class="question-box_header">
<input type="text" id="" class="question-box_header_question" style="color: black;" placeholder="Question"> <input type="text" id="" class="question-box_header_question" style="color: black;" placeholder="Question">
@ -41,7 +41,7 @@
<div id="options-container"> <div id="options-container">
<div class="question-box_option-block" id="option-template"> <div class="question-box_option-block" id="option-template">
<img id="question-type-image" src="<?= base_url() ?>assets/images/circle.png" alt="option circle" width="16px" height="16px"> <img id="question-type-image" src="<?= base_url() ?>assets/images/circle.png" alt="option circle" width="16px" height="16px">
<input type="text" placeholder="Option1" class="question-box_option-block_option-text"> <input id = "option-text" type="text" placeholder="Option1" class="question-box_option-block_option-text">
</div> </div>
<br> <br>
<!-- New options should be appended here --> <!-- New options should be appended here -->

View File

@ -6,23 +6,23 @@
<h1 class="text-center"><?= $title; ?></h1> <h1 class="text-center"><?= $title; ?></h1>
<div class="form-group"> <div class="form-group">
<label>Name</label> <label>Name</label>
<input type="text" class="form-control" name="name" placeholder="Name"> <input type="text" class="form-control" name="name" placeholder="Name" required autofocus>
</div> </div>
<div class="form-group"> <div class="form-group">
<label>Email</label> <label>Email</label>
<input type="email" class="form-control" name="email" placeholder="Email"> <input type="email" class="form-control" name="email" placeholder="Email" required autofocus>
</div> </div>
<div class="form-group"> <div class="form-group">
<label>Username</label> <label>Username</label>
<input type="text" class="form-control" name="username" placeholder="Username"> <input type="text" class="form-control" name="username" placeholder="Username" required autofocus>
</div> </div>
<div class="form-group"> <div class="form-group">
<label>Password</label> <label>Password</label>
<input type="password" class="form-control" name="password" placeholder="Password"> <input type="password" class="form-control" name="password" placeholder="Password" required autofocus>
</div> </div>
<div class="form-group"> <div class="form-group">
<label>Confirm Password</label> <label>Confirm Password</label>
<input type="password" class="form-control" name="password2" placeholder="Confirm Password"> <input type="password" class="form-control" name="password2" placeholder="Confirm Password" required autofocus>
</div> </div>
<button type="submit" class="btn btn-primary btn-block">Submit</button> <button type="submit" class="btn btn-primary btn-block">Submit</button>
</div> </div>

View File

@ -69,6 +69,8 @@
background-color: #fff; background-color: #fff;
padding: 30px 25px; padding: 30px 25px;
border-radius: 8px; border-radius: 8px;
margin-top: 10px;
margin-bottom: 10px;
} }

View File

@ -2,12 +2,14 @@ $(document).ready(function() {
console.log('jQuery is ready'); console.log('jQuery is ready');
let questionCount = 0; let questionCount = 0;
// Add new question
$('#add-question').click(function() { $('#add-question').click(function() {
questionCount++; questionCount++;
// Clone the question template // Clone the question template
var newQuestion = $('#question-template').clone(); var newQuestion = $('#question-template').clone();
newQuestion.removeAttr('id'); newQuestion.removeAttr('id');
newQuestion.attr('data-question-type', 'multiple-choice'); // Set default question type
newQuestion.find('.question-box_header_question').attr('placeholder', 'Question ' + questionCount); newQuestion.find('.question-box_header_question').attr('placeholder', 'Question ' + questionCount);
newQuestion.find('.question-box_option-block_option-text').attr('placeholder', 'Option 1'); newQuestion.find('.question-box_option-block_option-text').attr('placeholder', 'Option 1');
newQuestion.show(); // Ensure the cloned template is visible newQuestion.show(); // Ensure the cloned template is visible
@ -16,6 +18,7 @@ $(document).ready(function() {
$('#question-template').parent().append(newQuestion); $('#question-template').parent().append(newQuestion);
}); });
// Add new option to a question
$(document).on('click', '#add-option', function() { $(document).on('click', '#add-option', function() {
const questionBox = $(this).closest('.question-box'); const questionBox = $(this).closest('.question-box');
const currentQuestionType = questionBox.attr('data-question-type'); const currentQuestionType = questionBox.attr('data-question-type');
@ -39,16 +42,19 @@ $(document).ready(function() {
questionBox.find('#new-options').append(newOption).append('<br>'); questionBox.find('#new-options').append(newOption).append('<br>');
}); });
// Remove an option from a question
$(document).on('click', '.question-box_option-block_option-close', function() { $(document).on('click', '.question-box_option-block_option-close', function() {
$(this).closest('.question-box_option-block').next('br').remove(); $(this).closest('.question-box_option-block').next('br').remove();
$(this).closest('.question-box_option-block').remove(); $(this).closest('.question-box_option-block').remove();
}); });
// Delete a question
$(document).on('click', '.delete-question', function() { $(document).on('click', '.delete-question', function() {
$(this).closest('.question-box').next('br').remove(); $(this).closest('.question-box').next('br').remove();
$(this).closest('.question-box').remove(); $(this).closest('.question-box').remove();
}); });
// Duplicate a question
$(document).on('click', '.duplicate-question', function() { $(document).on('click', '.duplicate-question', function() {
const originalQuestion = $(this).closest('.question-box'); const originalQuestion = $(this).closest('.question-box');
const duplicateQuestion = originalQuestion.clone(); const duplicateQuestion = originalQuestion.clone();
@ -59,7 +65,7 @@ $(document).ready(function() {
originalQuestion.after(duplicateQuestion).after('<br>'); originalQuestion.after(duplicateQuestion).after('<br>');
}); });
// Event delegation for question type changes // Handle question type change
$(document).on('change', '#question-type', function() { $(document).on('change', '#question-type', function() {
const selectedType = $(this).val(); const selectedType = $(this).val();
const questionBox = $(this).closest('.question-box'); const questionBox = $(this).closest('.question-box');
@ -87,6 +93,7 @@ $(document).ready(function() {
questionBox.attr('data-question-type', selectedType); questionBox.attr('data-question-type', selectedType);
}).trigger('change'); }).trigger('change');
// Submit form
$('#submit-form').click(function() { $('#submit-form').click(function() {
var formData = { var formData = {
title: $('#form-title').val(), title: $('#form-title').val(),
@ -98,13 +105,13 @@ $(document).ready(function() {
var questionBox = $(this); var questionBox = $(this);
var questionData = { var questionData = {
question: questionBox.find('.question-box_header_question').val(), question: questionBox.find('.question-box_header_question').val(),
type: questionBox.find('.question-type').val(), type: questionBox.find('#question-type').val(),
options: [] options: []
}; };
if (questionData.type !== 'paragraph') { if (questionData.type !== 'paragraph') {
questionBox.find('.option-template').each(function() { questionBox.find('.question-box_option-block').each(function() {
var optionText = $(this).find('.option-text').val(); var optionText = $(this).find('.question-box_option-block_option-text').val();
if (optionText) { if (optionText) {
questionData.options.push(optionText); questionData.options.push(optionText);
} }
@ -114,6 +121,8 @@ $(document).ready(function() {
formData.questions.push(questionData); formData.questions.push(questionData);
}); });
console.log(formData);
$.ajax({ $.ajax({
url: base_url+'forms/submit_form', url: base_url+'forms/submit_form',
type: 'POST', type: 'POST',