diff --git a/application/config/database.php b/application/config/database.php index de9475a..e8ab51e 100644 --- a/application/config/database.php +++ b/application/config/database.php @@ -76,8 +76,8 @@ $query_builder = TRUE; $db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', - 'username' => 'root', - 'password' => '', + 'username' => 'jostheta', + 'password' => 'Pa$$w0rd', 'database' => 'gforms', 'dbdriver' => 'mysqli', 'dbprefix' => '', diff --git a/application/controllers/Forms.php b/application/controllers/Forms.php index f6580e2..f8120ea 100644 --- a/application/controllers/Forms.php +++ b/application/controllers/Forms.php @@ -223,7 +223,7 @@ class Forms extends CI_Controller $form_id = $this->input->post('form_id'); $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 = []; @@ -234,9 +234,9 @@ class Forms extends CI_Controller } if (!empty($errors)) { - $this->session->set_flashdata('errors', $errors); - $this->session->set_flashdata('responses', $responses); // Persisting responses - redirect('forms/respond_form/' . $form_id); // Redirect back to the form + $this->output + ->set_content_type('application/json') + ->set_output(json_encode(['success' => false, 'errors' => $errors])); } else { if ($this->Form_model->save_responses($form_id, $responses)) { $this->output @@ -250,6 +250,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 public function list_user_forms() { diff --git a/application/views/forms/respond_form.php b/application/views/forms/respond_form.php index fb828f7..3bc302e 100644 --- a/application/views/forms/respond_form.php +++ b/application/views/forms/respond_form.php @@ -7,6 +7,15 @@
description, ENT_QUOTES, 'UTF-8') ?>
+ + +
+ + + +
+ +
@@ -16,7 +25,7 @@ $responses = $this->session->flashdata('responses'); ?> $question): ?> -
@@ -36,7 +45,6 @@ -
diff --git a/application/views/forms/view_response.php b/application/views/forms/view_response.php index 9987733..92ecb17 100644 --- a/application/views/forms/view_response.php +++ b/application/views/forms/view_response.php @@ -1,23 +1,33 @@

-
+
-
title, ENT_QUOTES, 'UTF-8') ?>
-
description, ENT_QUOTES, 'UTF-8') ?>
+
+ title, ENT_QUOTES, 'UTF-8') ?> +
+
+ description, ENT_QUOTES, 'UTF-8') ?> +

-
Response ID: response_id ?>
-
Submitted At: created_at)) ?>
+
+ Response ID: response_id ?> +
+
+ Submitted At: created_at)) ?> +
- +
$question) : ?>
-
question_text, ENT_QUOTES, 'UTF-8') ?>
+
+ question_text, ENT_QUOTES, 'UTF-8') ?> +

+ question_type == 'dropdown') : ?> +
+ +
options)) : ?> options as $optionIndex => $option) : ?> -
+
question_type == 'multiple-choice') : ?> -  option_text, ENT_QUOTES, 'UTF-8'), $answer_texts)) ? 'checked' : '' ?> readonly> +  option_text, ENT_QUOTES, 'UTF-8'), $answer_texts)) ? 'checked' : '' ?> disabled> question_type == 'checkbox') : ?> -  option_text, ENT_QUOTES, 'UTF-8'), $answer_texts)) ? 'checked' : '' ?> readonly> +  option_text, ENT_QUOTES, 'UTF-8'), $answer_texts)) ? 'checked' : '' ?> disabled>
diff --git a/assets/js/.script.js.swp b/assets/js/.script.js.swp deleted file mode 100644 index ff24d31..0000000 Binary files a/assets/js/.script.js.swp and /dev/null differ diff --git a/assets/js/script.js b/assets/js/script.js index 1610b35..73fa058 100644 --- a/assets/js/script.js +++ b/assets/js/script.js @@ -253,17 +253,27 @@ $(document).ready(function() { $(document).ready(function() { $('#response-form').on('submit', function(e) { e.preventDefault(); - + var form = $(this); + $.ajax({ - url: $(this).attr('action'), - type: $(this).attr('method'), - data: $(this).serialize(), + url: form.attr('action'), + type: form.attr('method'), + data: form.serialize(), dataType: 'json', success: function(data) { if (data.success) { alert('Response submitted successfully!'); // Optionally, you can clear the form or redirect the user 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('
' + error_message + '
'); + }); } else { alert('An error occurred. Please try again.'); } @@ -274,4 +284,5 @@ $(document).ready(function() { }); }); }); + }); \ No newline at end of file