Please consider the advantages to this alternative approach. Your booking form could be setup using multiple "pages", which only appear as pages to the user.
jsFiddle example
That is, to the user, he/she would be going from one screen to the next, but that would be an illusion created by javascript (e.g. jQueryUI Tabs). In reality, all fields are in the same page/same form, as in the jsFiddle. Then, you could collect all information when user hits the Submit button and:
- verify all fields are completed;
- verify correct format for data in fields;
- if anything wrong, (1) display message via
alert()
or jQueryUI dialog, and (2) send user back to the form via return false;
- if nothing wrong, submit form via:
$('#IdOfForm').submit();
By doing all data collection and verification client-side, you can greatly simplify your PHP code. Also, if user is sent back to complete missed fields, all the data they typed so far is still there...
Note that you must still verify the data on the PHP side to prevent SQL injection. See this article and this SO Post for more information.
Stand-alone, working code example:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<style>
</style>
<script type="text/javascript">
$(document).ready(function() {
var arrFormFields = ['cn','ca','ce','cp','dd','rd'];
$("#tabs").tabs();
$("#theMsg").dialog({
autoOpen: false,
});
$('#doit').click(function() {
for (var fld in arrFormFields) {
if ( $('#' + arrFormFields[fld]).val() == ''){
console.log('Var ' +arrFormFields[fld]+ ' has value: ' + $('#'+arrFormFields[fld]).val() );
$('#' + arrFormFields[fld]).css({'border':'1px solid red','background':'yellow'})
$('#ui-id-1').trigger('click');
$('#' + arrFormFields[fld]).focus();
alert('Please complete all fields'); //For some reason the alert isn't displaying
return false;
}else{
//Submit form to "dothebooking.php", like this:
//$('#TripForm').submit();
$('#theMsg').html('<h2>Form was successfully submitted</h2>');
$('#theMsg').dialog({'title':'Notice'}).dialog('open');
}
}
});
$('input:text').blur(function() {
$("input:not(:button)").css({'border':'1px solid grey','background':'white'});
});
}); //END $(document).ready()
</script>
</head>
<body>
<div id="tabs">
<ul>
<li><a href="#tabs-1">Customer Details</a></li>
<li><a href="#tabs-2">Trip Details</a></li>
<li><a href="#tabs-3">Trip Options</a></li>
</ul>
<form id='TripForm' action="dothebooking.php" method="POST">
<div id="tabs-1">
Customer Name: <input type='text' id="cn"><br>
Customer Address: <input type='text' id="ca"><br>
Customer Email: <input type='text' id="ce"><br>
Customer Phone: <input type='text' id="cp"><br>
</div><!-- #tabs-1 -->
<div id="tabs-2">
Departure Date: <input type='text' id="dd"><br>
Return Date: <input type='text' id="rd"><br>
</div><!-- #tabs-2 -->
<div id="tabs-3">
Guided Tour option <input type='checkbox' id="gt"><br>
Presidential Suite <input type='checkbox' id="ps"><br>
Bed and Breakfast <input type='checkbox' id="bb"><br>
<input type='button' id="doit" value="Submit Trip"><br>
</div><!-- #tabs-3 -->
</form><!-- #TripForm -->
</div><!-- #tabs -->
<div id="theMsg"></div>
</body>
</html>
The jQuery validation plugin might also be of interest to you.
Check out these demos for a quick look/see.