I have done some javascript validation for my HTML form but I need to include some PHP validation as well, so I included the PHP validation for all the features in the HTML form when the user forget to enter something in a text input or select a radio button.
Problem is that if the user clicks on the “Prepare Question” submit button, then it doesn’t display the echos when it hould do, instead it submits the form, I don’t want this to happen. I don’t want the form to be submitted until the php validation is met successfully. What do I need to change in my code in order for this to work:
Below is the code:
<form action="QandATable.php" method="post" id="sessionForm">
<p><strong>1: Your Assessment ID: </strong><span id="idFont"><?php echo $id; ?></span></p>
<input type='hidden' name='id' value='<?php echo $id; ?>' />
<p><strong>2: Number of Assessments you Require:</strong> <input type="text" id="sessionNo" name="sessionNum" onkeypress="return isNumberKey(event)" maxlength="5" /></p>
<p><strong>3: Duration:</strong> <input type="text" id="durationpicker" name="durationChosen" readonly="readonly" /></p>
<p><strong>4: Date:</strong> <input type="text" id="datepicker" name="dateChosen" readonly="readonly" /></p>
<p><strong>5: Start Time:</strong> <input type="text" id="timepicker" name="timeChosen" readonly="readonly" /><span class="timepicker_button_trigger"><img src="Images/clock.gif" alt="Choose Time" /></span></p>
<p><strong>6: </strong><input class="questionBtn" type="submit" value="Prepare Questions" name="prequestion" onClick="myClickHandler(); return false;"/></p> <!-- Prepare Questions here-->
</form>
<?php
if (isset($_POST['prequestion'])) {
if(empty($_POST['sessionNum'])) {
echo "Please Set the Number of Assessments";
}
elseif(empty($_POST['durationChosen'])) {
echo "Please Select your Assessment's Duration";
}
elseif(empty($_POST['dateChosen'])) {
echo "Please Select a Date";
}
elseif(empty($_POST['timeChosen'])) {
echo "Please Select a Start Time";
}
else {
?>
<script type="text/javascript">
function myClickHandler(){
if(validation()){
showConfirm();
}
}
<?php
}
}
?>
PHP is a server-side scripting language so there is no straight forward way you can do a PHP validation on a form before it is submitted.
What you can do, if you have to do PHP validation for one reason or another, is to submit the field values asynchronously (in a non visible way). You could f ex use javascript to call a GET on a hidden iframe with the form data and return errors from a separate php page (f ex calling /hidden/formcheck.php?sessionNum=XX&(…) and return false or true).
If you really want to do it in that way you’d be much better off using jQuery or some similar library who has a tonne of ready made features for remote URL calling (like jQuery’s http://api.jquery.com/load/ )