CodeIgniter_Gforms/assets/js/script.js

139 lines
5.3 KiB
JavaScript
Raw Normal View History

2024-07-11 13:04:55 +00:00
$(document).ready(function() {
console.log('jQuery is ready');
let questionCount = 0;
2024-07-12 09:55:29 +00:00
// Add new question
2024-07-11 13:04:55 +00:00
$('#add-question').click(function() {
questionCount++;
2024-07-12 06:21:27 +00:00
2024-07-11 13:04:55 +00:00
// Clone the question template
var newQuestion = $('#question-template').clone();
newQuestion.removeAttr('id');
2024-07-12 09:55:29 +00:00
newQuestion.attr('data-question-type', 'multiple-choice'); // Set default question type
2024-07-11 13:04:55 +00:00
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
// Append the cloned question to the form container
2024-07-12 06:21:27 +00:00
$('#question-template').parent().append(newQuestion);
2024-07-11 13:04:55 +00:00
});
2024-07-12 09:55:29 +00:00
// Add new option to a question
2024-07-11 13:04:55 +00:00
$(document).on('click', '#add-option', function() {
const questionBox = $(this).closest('.question-box');
const currentQuestionType = questionBox.attr('data-question-type');
let optionCount = questionBox.find('.question-box_option-block').length + 1;
var newOption = $('#option-template').clone();
newOption.removeAttr('id');
2024-07-12 06:21:27 +00:00
newOption.find('input').val('');
newOption.find('input').attr('placeholder', 'Option ' + optionCount);
2024-07-11 13:04:55 +00:00
if (currentQuestionType === 'multiple-choice') {
newOption.find('img').attr('src', base_url+'assets/images/circle.png');
} else if (currentQuestionType === 'checkbox') {
newOption.find('img').attr('src', base_url+'assets/images/square.png');
}
if (optionCount > 1) {
newOption.append('<button class="question-box_option-block_option-close"><img src="'+base_url+'assets/images/close.png" alt="close option"></button>');
}
questionBox.find('#new-options').append(newOption).append('<br>');
});
2024-07-12 09:55:29 +00:00
// Remove an option from a question
2024-07-11 13:04:55 +00:00
$(document).on('click', '.question-box_option-block_option-close', function() {
2024-07-12 06:21:27 +00:00
$(this).closest('.question-box_option-block').next('br').remove();
$(this).closest('.question-box_option-block').remove();
2024-07-11 13:04:55 +00:00
});
2024-07-12 09:55:29 +00:00
// Delete a question
2024-07-11 13:04:55 +00:00
$(document).on('click', '.delete-question', function() {
2024-07-12 06:21:27 +00:00
$(this).closest('.question-box').next('br').remove();
$(this).closest('.question-box').remove();
2024-07-11 13:04:55 +00:00
});
2024-07-12 09:55:29 +00:00
// Duplicate a question
2024-07-11 13:04:55 +00:00
$(document).on('click', '.duplicate-question', function() {
2024-07-12 06:21:27 +00:00
const originalQuestion = $(this).closest('.question-box');
2024-07-11 13:04:55 +00:00
const duplicateQuestion = originalQuestion.clone();
duplicateQuestion.removeAttr('id');
duplicateQuestion.show();
originalQuestion.after(duplicateQuestion).after('<br>');
});
2024-07-12 09:55:29 +00:00
// Handle question type change
2024-07-12 06:21:27 +00:00
$(document).on('change', '#question-type', function() {
2024-07-11 13:04:55 +00:00
const selectedType = $(this).val();
2024-07-12 06:21:27 +00:00
const questionBox = $(this).closest('.question-box');
const image = questionBox.find('#question-type-image');
const optionsContainer = questionBox.find('#options-container');
const shortAnswerContainer = questionBox.find('.question-box_short-answer');
2024-07-11 13:04:55 +00:00
if (selectedType === 'multiple-choice') {
image.attr('src', base_url+'assets/images/circle.png');
image.attr('alt', 'Circle for Multiple Choice');
optionsContainer.show();
shortAnswerContainer.hide();
} else if (selectedType === 'checkbox') {
image.attr('src', base_url+'assets/images/square.png');
image.attr('alt', 'Square for Checkbox');
optionsContainer.show();
shortAnswerContainer.hide();
} else if (selectedType === 'paragraph') {
image.attr('src', '');
image.attr('alt', '');
optionsContainer.hide();
shortAnswerContainer.show();
}
2024-07-12 06:21:27 +00:00
questionBox.attr('data-question-type', selectedType);
}).trigger('change');
2024-07-11 13:04:55 +00:00
2024-07-12 09:55:29 +00:00
// Submit form
2024-07-12 06:21:27 +00:00
$('#submit-form').click(function() {
var formData = {
title: $('#form-title').val(),
description: $('#form-desc').val(),
questions: []
};
2024-07-11 13:04:55 +00:00
$('.question-box:visible').each(function() {
2024-07-12 06:21:27 +00:00
var questionBox = $(this);
var questionData = {
question: questionBox.find('.question-box_header_question').val(),
2024-07-12 09:55:29 +00:00
type: questionBox.find('#question-type').val(),
2024-07-12 06:21:27 +00:00
options: []
};
if (questionData.type !== 'paragraph') {
2024-07-12 09:55:29 +00:00
questionBox.find('.question-box_option-block').each(function() {
var optionText = $(this).find('.question-box_option-block_option-text').val();
2024-07-12 06:21:27 +00:00
if (optionText) {
questionData.options.push(optionText);
}
});
}
2024-07-11 13:04:55 +00:00
2024-07-12 06:21:27 +00:00
formData.questions.push(questionData);
});
2024-07-11 13:04:55 +00:00
2024-07-12 09:55:29 +00:00
console.log(formData);
2024-07-12 06:21:27 +00:00
$.ajax({
url: base_url+'forms/submit_form',
type: 'POST',
data: { formData: JSON.stringify(formData) },
success: function(response) {
console.log('Form data submitted successfully:', response);
},
error: function(xhr, status, error) {
console.error('Error submitting form data:', error);
}
});
});
2024-07-12 09:55:29 +00:00
});