form_page.html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="js/process_truck_req.js"></script>
<script src="js/jquery-1.2.3.pack.js"></script>
<script src="js/runonload.js"></script>
</head>
<div class="prform" id ="request_form">
<form name="truckreq" action="" method="post" class="truckreq_form">
<label for="seltruck" id="seltruck_label"><font class="whitetext">Select Truck</font></label><br />
<select name="seltruck" id="seltruck">
<option value="Select a Truck"> Select a Truck</option>
<option value="2011+Tacoma">2011 Tacoma</option>
<option value="2008+Tundra">2008 Tundra</option>
<option value="2000+Tacoma">2000 Tacoma</option>
</select><br />
<label class="error" for="seltruck" id="seltruck_error"><font class="redtext">This field is required.</font></label><br />
<label class="error" for="seltruck" id="seltruck_noavail_error"><font class="redtext">Not Available on selected Dates.</font></label><br />
</form>
process_request.js
$(function() {
$('.error').hide();
$('input.text-input').css({backgroundColor:"#FFFFFF"});
$('input.text-input').focus(function(){
$(this).css({backgroundColor:"#FFDDAA"});
});
$('input.radio-input').focus(function(){
$(this).css({backgroundColor:"#FFDDAA"});
});
$('input.text-input').blur(function(){
$(this).css({backgroundColor:"#FFFFFF"});
});
$(".button").click(function() {
// validate and process form
// first hide any error messages
$('.error').hide();
var seltruck = $("#seltruck").val();
if (seltruck == "Select a Truck") {
$("label#seltruck_error").show();
$("#seltruck").focus();
return false;
}
var truckSearch = 'seltruck=' + seltruck + '&outdate=' + outdate + '&indate=' + indate;
$.ajax({
type: "POST",
url: "do_truck_search.php",
data: truckSearch,
success: function() {
var truck_status = $("#truck_status").val();
if (truck_status == "nopass") {
$("label#seltruck_noavail_error").show();
$("#seltruck").focus();
return false;
}
}
});
});
});
runOnLoad(function() {
$("input#projdesc").select().focus();
});
Take input form data from form_page.html, pass to process_request.js for validation. I only displayed seltruck, other form fields are set in form_page.html.
At the .js validation, the fields are check if they are filled out, if not, the error label class is displayed on the form_page.html.
The seltruck form field requires mysql to be queried and checked for availability. I have the do_truck_search.php script working great, but don’t know how to pass the ‘truck_status’ variable from do_truck_search.php back to the .ajax call.
Once back at the .ajax call, I’d like a success: ‘continue’ or error: display the label#seltruck_noavail_error.
any help?
thanks!
UPDATE – can’t get this to work? dataType: “text” in .ajax works though? any thoughts?
do_truck_search.php
if (($unixoutdate >= $dbunixoutdate) && ($unixoutdate <= $dbunixindate) && ($dbtruck == $seltruck_final)){
$truck_status = "nopass";
$data2 = array('truck_status' => $truck_status);
echo json_encode($data2);
}
process_request.js:
$.ajax({
type: "POST",
url: "do_truck_search.php",
data: truckString,
dataType: "json",
success: function(data) {
if (data.truck_status == "nopass"){
$("label#seltruck_noavail_error").show();
}
}
});
UPDATE
I think the reason why the json datatype was unreliable is because a small square (probably a space) is echo’d from the PHP script. Using datatype: ‘text’ and an alert() in the .ajax success callback shows the small square, prior to the actual data text. My dirty solution was to use datatype: text, then just substr the actual data I want to retrieve.
I searched hi/low in the PHP script to find the cause of the echo’d space, but couldn’t find it??
One possible way to do this:
$.ajaxsetdataType: "json"echo json_encode(array("truck_status" => $truck_status));. Note that no other output must be present (so disable your layouts, views etc.)success: function() {...}tosuccess: function(data, status) {...}; now the variabledatawill contain the keytruck_statuswith whatever you set it to. So access it usingdata.truck_status.