For each question, I want to compile an if statement where by checking through each question if the .ReplyType text input equals Single, then the user can only select a single checkbox button option, otherwise if the .ReplyType text input equals Multiple, then the user can select multiple checkbox button options.
My question is how to implement this if a single button or multiple buttons are selected depending on the .ReplyType text input? Is it better in PHP or JavaScript?
Below is relevant code:
<form action='results.php' method='post' id='exam'>
<?php
//COMPILE OPTION BUTTONS
function ExpandOptionType($option) {
$options = explode('-', $option);
if(count($options) > 1) {
$start = array_shift($options);
$end = array_shift($options);
do {
$options[] = $start;
}while(++$start <= $end);
}
else{
$options = explode(' or ', $option);
}
foreach($options as $indivOption) {
echo '<div class="ck-button"><label class="fixedLabelCheckbox"><input type="checkbox" name="options[]" id="option-' . $indivOption . '" value="' . $indivOption . '" /><span>' . $indivOption . '</span></label></div>';
}
}
foreach ($arrQuestionId as $key=>$question) {
?>
<div class="queWrap">
//DISPLAY QUESTION NUMBER FOR EACH QUESTION
<p><?php echo htmlspecialchars($arrQuestionNo[$key]);?></p>
//DISPLAY OPTION CHECKBOX BUTTONS FOR EACH QUESTION
<?php echo ExpandOptionType(htmlspecialchars($arrOptionType[$key])); ?>
//DISPLAY REPLY TYPE TEXT INPUT FOR EACH QUESTION
<p>Reply Type: <input type='text' class='replyType' name='replytype' value='<?php echo htmlspecialchars($arrReplyType[$key]); ?>' /></p>
</div>
<?php
}
?>
You should do both, JavaScript and PHP implementation.
JavaScript is better for the user to get a response before submitting the form.
PHP is important, because JavaScript validation can be bypassed (just deactivate JavaScript) in this case your PHP should still validate that you get consistent data.
Anyway: Use radio-buttons for single-select and checkboxes for multi-select. Not that it’s already implemented in every browser and meant to be used that way, most users have already learned this behavious and won’t be surprised by your JavaScript (or PHP) validation.