fixed req and drop down

This commit is contained in:
jostheta 2024-07-23 10:31:02 +05:30
parent b32e2c821e
commit 76104ff94e
6 changed files with 86 additions and 22 deletions

View File

@ -76,8 +76,8 @@ $query_builder = TRUE;
$db['default'] = array( $db['default'] = array(
'dsn' => '', 'dsn' => '',
'hostname' => 'localhost', 'hostname' => 'localhost',
'username' => 'root', 'username' => 'jostheta',
'password' => '', 'password' => 'Pa$$w0rd',
'database' => 'gforms', 'database' => 'gforms',
'dbdriver' => 'mysqli', 'dbdriver' => 'mysqli',
'dbprefix' => '', 'dbprefix' => '',

View File

@ -223,7 +223,7 @@ class Forms extends CI_Controller
$form_id = $this->input->post('form_id'); $form_id = $this->input->post('form_id');
$responses = $this->input->post('responses'); $responses = $this->input->post('responses');
$questions = $this->Form_model->get_questions_by_form_id($form_id); // Assuming you have a method to get questions by form_id $questions = $this->Form_model->get_questions_by_form_id($form_id);
$errors = []; $errors = [];
@ -234,9 +234,9 @@ class Forms extends CI_Controller
} }
if (!empty($errors)) { if (!empty($errors)) {
$this->session->set_flashdata('errors', $errors); $this->output
$this->session->set_flashdata('responses', $responses); // Persisting responses ->set_content_type('application/json')
redirect('forms/respond_form/' . $form_id); // Redirect back to the form ->set_output(json_encode(['success' => false, 'errors' => $errors]));
} else { } else {
if ($this->Form_model->save_responses($form_id, $responses)) { if ($this->Form_model->save_responses($form_id, $responses)) {
$this->output $this->output
@ -251,6 +251,30 @@ class Forms extends CI_Controller
} }
public function respond_form($form_id) {
$this->load->model('Form_model');
$form = $this->Form_model->get_form_by_id($form_id);
$questions = $this->Form_model->get_questions_by_form_id($form_id);
$responses = $this->session->flashdata('responses');
$errors = $this->session->flashdata('errors');
$success = $this->session->flashdata('success');
$error = $this->session->flashdata('error');
$data = [
'form' => $form,
'questions' => $questions,
'responses' => $responses,
'errors' => $errors,
'success' => $success,
'error' => $error,
];
$this->load->view('forms/respond_form', $data);
}
// List all forms of the current logged-in user // List all forms of the current logged-in user
public function list_user_forms() { public function list_user_forms() {
$user_id = $this->session->userdata('user_id'); $user_id = $this->session->userdata('user_id');

View File

@ -7,6 +7,15 @@
<div class="form_container_top_desc"><?= htmlspecialchars($form->description, ENT_QUOTES, 'UTF-8') ?> <div class="form_container_top_desc"><?= htmlspecialchars($form->description, ENT_QUOTES, 'UTF-8') ?>
</div> </div>
</div> </div>
<?php if (isset($success)): ?>
<div class="alert alert-success"><?= $success ?></div>
<?php endif; ?>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?= $error ?></div>
<?php endif; ?>
<form id="response-form" action="<?= base_url('forms/submit_response') ?>" method="post"> <form id="response-form" action="<?= base_url('forms/submit_response') ?>" method="post">
<input type="hidden" name="form_id" value="<?= $form->form_id ?>"> <input type="hidden" name="form_id" value="<?= $form->form_id ?>">
<div id="questions-container"> <div id="questions-container">
@ -16,7 +25,7 @@
$responses = $this->session->flashdata('responses'); $responses = $this->session->flashdata('responses');
?> ?>
<?php foreach ($questions as $index => $question): ?> <?php foreach ($questions as $index => $question): ?>
<div class="question-box" <div class="question-box" data-question-id="<?= $question->question_id ?>"
data-question-type="<?= htmlspecialchars($question->question_type, ENT_QUOTES, 'UTF-8') ?>" data-question-type="<?= htmlspecialchars($question->question_type, ENT_QUOTES, 'UTF-8') ?>"
data-required="<?= $question->is_required ? 'true' : 'false' ?>"> data-required="<?= $question->is_required ? 'true' : 'false' ?>">
<div class="question-box_header"> <div class="question-box_header">
@ -36,7 +45,6 @@
<textarea class="response-text-area" style="color:black;font-style:normal;" <textarea class="response-text-area" style="color:black;font-style:normal;"
name="responses[<?= $question->question_id ?>]" name="responses[<?= $question->question_id ?>]"
placeholder="Your Answer"><?php echo isset($responses[$question->question_id]) ? htmlspecialchars($responses[$question->question_id], ENT_QUOTES, 'UTF-8') : ''; ?></textarea> placeholder="Your Answer"><?php echo isset($responses[$question->question_id]) ? htmlspecialchars($responses[$question->question_id], ENT_QUOTES, 'UTF-8') : ''; ?></textarea>
</div> </div>
<?php else: ?> <?php else: ?>
<div id="options-container"> <div id="options-container">

View File

@ -3,12 +3,20 @@
<div class="section"> <div class="section">
<div class="form_container-response"> <div class="form_container-response">
<div class="form_container_top"> <div class="form_container_top">
<div class = "form_container_top_title" style="border-bottom:none;"><?= htmlspecialchars($form->title, ENT_QUOTES, 'UTF-8') ?></div> <div class="form_container_top_title" style="border-bottom:none;">
<div class = "form_container_top_desc" style="border-bottom:none;"><?= htmlspecialchars($form->description, ENT_QUOTES, 'UTF-8') ?></div> <?= htmlspecialchars($form->title, ENT_QUOTES, 'UTF-8') ?>
</div>
<div class="form_container_top_desc" style="border-bottom:none;">
<?= htmlspecialchars($form->description, ENT_QUOTES, 'UTF-8') ?>
</div>
<div> <div>
<br> <br>
<div class = "form_container_top_user-details">Response ID: <?= $response->response_id ?></div> <div class="form_container_top_user-details">
<div class = "form_container_top_user-details">Submitted At: <?= date('Y-m-d H:i:s', strtotime($response->created_at)) ?></div> Response ID: <?= $response->response_id ?>
</div>
<div class="form_container_top_user-details">
Submitted At: <?= date('Y-m-d H:i:s', strtotime($response->created_at)) ?>
</div>
</div> </div>
</div> </div>
@ -17,7 +25,9 @@
<?php foreach ($questions as $index => $question) : ?> <?php foreach ($questions as $index => $question) : ?>
<div class="question-box" data-question-type="<?= htmlspecialchars($question->question_type, ENT_QUOTES, 'UTF-8') ?>"> <div class="question-box" data-question-type="<?= htmlspecialchars($question->question_type, ENT_QUOTES, 'UTF-8') ?>">
<div class="question-box_header"> <div class="question-box_header">
<div class="response-questions" style="color:black;"><?= htmlspecialchars($question->question_text, ENT_QUOTES, 'UTF-8') ?></div> <div class="response-questions" style="color:black;">
<?= htmlspecialchars($question->question_text, ENT_QUOTES, 'UTF-8') ?>
</div>
</div> </div>
<br> <br>
<?php <?php
@ -32,16 +42,27 @@
<div class="question-box_short-answer"> <div class="question-box_short-answer">
<textarea class="response-text-area" name="responses[<?= $question->question_id ?>]" placeholder="Paragraph" readonly><?= implode("\n", $answer_texts) ?></textarea> <textarea class="response-text-area" name="responses[<?= $question->question_id ?>]" placeholder="Paragraph" readonly><?= implode("\n", $answer_texts) ?></textarea>
</div> </div>
<?php elseif ($question->question_type == 'dropdown') : ?>
<div class="question-box_dropdown">
<select name="responses[<?= $question->question_id ?>]" class="form-control" disabled>
<option value="">Select an option</option>
<?php foreach ($question->options as $option) : ?>
<option value="<?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?>" <?= in_array(htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8'), $answer_texts) ? 'selected' : '' ?>>
<?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?>
</option>
<?php endforeach; ?>
</select>
</div>
<?php else : ?> <?php else : ?>
<div id="options-container"> <div id="options-container">
<?php if (!empty($question->options)) : ?> <?php if (!empty($question->options)) : ?>
<?php foreach ($question->options as $optionIndex => $option) : ?> <?php foreach ($question->options as $optionIndex => $option) : ?>
<div class="question-box_option-block" id="option-template" data-option_id="<?= htmlspecialchars($option->option_id, ENT_QUOTES, 'UTF-8') ?>"> <div class="question-box_option-block" id="option-template" data-option_id="<?= htmlspecialchars($option->option_id, ENT_QUOTES, 'UTF-8') ?>">
<?php if ($question->question_type == 'multiple-choice') : ?> <?php if ($question->question_type == 'multiple-choice') : ?>
&nbsp;<input type="radio" id="option-<?= $optionIndex ?>" name="responses[<?= $question->question_id ?>]" value="<?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?>" <?= (in_array(htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8'), $answer_texts)) ? 'checked' : '' ?> readonly> &nbsp;<input type="radio" id="option-<?= $optionIndex ?>" name="responses[<?= $question->question_id ?>]" value="<?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?>" <?= (in_array(htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8'), $answer_texts)) ? 'checked' : '' ?> disabled>
<label style="padding-top:12px;" for="option-<?= $optionIndex ?>"><?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?></label> <label style="padding-top:12px;" for="option-<?= $optionIndex ?>"><?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?></label>
<?php elseif ($question->question_type == 'checkbox') : ?> <?php elseif ($question->question_type == 'checkbox') : ?>
&nbsp;<input type="checkbox" id="option-<?= $optionIndex ?>" name="responses[<?= $question->question_id ?>][]" value="<?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?>" <?= (in_array(htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8'), $answer_texts)) ? 'checked' : '' ?> readonly> &nbsp;<input type="checkbox" id="option-<?= $optionIndex ?>" name="responses[<?= $question->question_id ?>][]" value="<?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?>" <?= (in_array(htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8'), $answer_texts)) ? 'checked' : '' ?> disabled>
<label style="padding-top:12px;" for="option-<?= $optionIndex ?>"><?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?></label> <label style="padding-top:12px;" for="option-<?= $optionIndex ?>"><?= htmlspecialchars($option->option_text, ENT_QUOTES, 'UTF-8') ?></label>
<?php endif; ?> <?php endif; ?>
</div> </div>

Binary file not shown.

View File

@ -253,17 +253,27 @@ $(document).ready(function() {
$(document).ready(function() { $(document).ready(function() {
$('#response-form').on('submit', function(e) { $('#response-form').on('submit', function(e) {
e.preventDefault(); e.preventDefault();
var form = $(this);
$.ajax({ $.ajax({
url: $(this).attr('action'), url: form.attr('action'),
type: $(this).attr('method'), type: form.attr('method'),
data: $(this).serialize(), data: form.serialize(),
dataType: 'json', dataType: 'json',
success: function(data) { success: function(data) {
if (data.success) { if (data.success) {
alert('Response submitted successfully!'); alert('Response submitted successfully!');
// Optionally, you can clear the form or redirect the user // Optionally, you can clear the form or redirect the user
window.location.href = base_url + 'my_forms'; window.location.href = base_url + 'my_forms';
} else if (data.errors) {
// Clear previous error messages
$('.error-message').remove();
// Display validation errors
$.each(data.errors, function(question_id, error_message) {
var questionBox = $('div[data-question-id="' + question_id + '"]');
questionBox.append('<div class="error-message" style="color:red;">' + error_message + '</div>');
});
} else { } else {
alert('An error occurred. Please try again.'); alert('An error occurred. Please try again.');
} }
@ -274,4 +284,5 @@ $(document).ready(function() {
}); });
}); });
}); });
}); });