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

View File

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

View File

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

View File

@ -10,7 +10,7 @@
<br>
<!-- 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 -->
<div class="question-box_header">
<input type="text" id="" class="question-box_header_question" style="color: black;" placeholder="Question">
@ -41,7 +41,7 @@
<div id="options-container">
<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">
<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>
<br>
<!-- New options should be appended here -->

View File

@ -6,23 +6,23 @@
<h1 class="text-center"><?= $title; ?></h1>
<div class="form-group">
<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 class="form-group">
<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 class="form-group">
<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 class="form-group">
<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 class="form-group">
<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>
<button type="submit" class="btn btn-primary btn-block">Submit</button>
</div>

View File

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

View File

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