title: Validate Form Data With Combination of Client Values and Ajax
body: <p>I have spent quite a lot of time trying to figure this out myself but am new to using jquery and ajax and it is probably something simple...</p>
  
  <p>I have a quote request form that I want to submit and I want to validate the details before submission. Some of this can be done in simple javascript and that part is working fine. I also need to get some values back from a mysql database and validate those which is where I am having problems.</p>
  
  <p>On my HTML form, I have this line</p>
  
  <pre><code>&lt;form id="quoteform" onsubmit=" return validateHire()" method="post" action="quote-results.php"&gt;
  </code></pre>
  
  <p>Which calls a validation script which is contained in a separate file which is included in the page containing the form.</p>
  
  <p>The validateHire script is as follows:</p>
  
  <pre><code>    function validateHire(){
  
  alert("Hire Validation Script Being Called!");
  
  // set the colllection date and time
  var collectionDate = new Date ($("#collectiondate").datepicker('getDate'));
  var collectionHour = ($("#collectionTime").val()).substr(0,2);
  collectionDate.setHours(collectionHour);
  var collectionMinute = ($("#collectionTime").val()).substr(3,4);
  collectionDate.setMinutes(collectionMinute);
  
  // set the return date and time
  var returnDate = new Date ($("#returndate").datepicker('getDate'));
  var returnHour = ($("#returnTime").val()).substr(0,2);
  returnDate.setHours(returnHour);
  var returnMinute = ($("#returnTime").val()).substr(3,4);
  returnDate.setMinutes(returnMinute);
  
  // calculate the length of hire and add additional day if necessary
  var timeDiff = returnHour - collectionHour
  var totaldays = Math.floor(((returnDate - collectionDate)/1000)/60/60/24);
  
  if(totaldays &lt;1 &amp;&amp; totaldays &gt;-1){
  totaldays=1;
  }
  
  // if(timeDiff &gt; 2){
  totaldays = totaldays + 1;
  alert(totaldays)
  }
  
  // get the collection and return day to check if it is a weekend hire
  var collectionDay = collectionDate.getDay();
  var returnDay = returnDate.getDay();
  
  // validate if the hire is a friday or saturday it meets minimum length
  if(collectionDay == 5 &amp;&amp; totaldays &lt;3){
  $("#errorbox").slideDown('fast');
  $("#errormessage").html("Hires Starting on a Friday Must be a Minimum of 3 Days");
  return false;
  }
  
  if(collectionDay == 6 &amp;&amp; totaldays &lt;2){
  $("#errorbox").slideDown('fast');
  $("#errormessage").html("Hires Starting on a Saturday Must be a Minimum of 2 Days");
  return false;
  }
  
  checkServerSide();
  function checkServerSide()
  {
  alert("now checking server side!");
  $.post("sproc-test.php",{q: totaldays, cdt: collectionDate, cTime: collectionTime, cLoc: CollectionLocation, cDay: collectionDay, cDas: collectionDateAsString, rDate: returnDate, rTime: returnTime, rLoc: returnLocation, rDay: returnDay, rdas: returnDateAsString},
  function(data){
  var minHire = data.minhire;
  alert(minHire); 
  
  $('#testvalue').val(minHire);
  
            }, "json"); 
  alert("This is the end of the check server side function");
  }
  
  if($('#testvalue').val() == 1){
  $("#errorbox").slideDown('fast');
  $("#errormessage").html("The Hire is not long enough to meet the requried minimum!");
  return false;
  }
  
  
  }
  </code></pre>
  
  <p>The first part works fine and prompts the user with the correct error message and the form is not submitted however when it comes to the checkServerSide function I have problems.</p>
  
  <p>The php script should return an array which I then try to access after the post and take a value and set it to a hidden form field so that I can validate against that however, according to Firebug, there is nothing actually being posted to the page that I am trying to submit the form too so wondering if my $post syntax is just wrong? also, the alert before the $post call works but the one after doesn't also leading me to wonder if I have messed something up here? although when looking in the firefox error console, there are not errors being reported.</p>
  
  <p>I hope I have sufficiently explained what I am trying to achieve but I have not been clear enough then please let me know. Also, as I mentioned I am very new to this so I would really appreciate an easy to understand explanation of what I am doing wrong and if there is a better way to achieve this.</p>
  
  <p>Huge thanks for any assistance! :)</p>
  
  <p>Kind Regards</p>
  
  <p>Mike</p>