0

Ok in summary what I am trying to do is disable a submit button based on an ajax call which is checking my database to see if data exists under a certain date.

At present I have got all of the above working however I am falling at the final hurdle in actually disabling the submit button. At presnt I have the response of my ajax call being displayed as text in a div on my page and I can by altering the code below, disable the button if the response does not match that being checked by my code but cannot get it working correctly be matching(or not the response). I guess my problem is that what I am checking and what the actual response from the ajax call is are different. Any help would be much appreciated.

My page that makes the ajax call

<script type="text/javascript">
function showHint(str)
{
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="";
  document.getElementById("txtHint").style.border="0px";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    document.getElementById("txtHint").style.border="1px solid #A5ACB2";
    if (xmlhttp.responseText == 'WARNING!! - PREVIOUS RECORDS FOUND') document.getElementById('FRINSUB').disabled = true;
    else
    document.getElementById('FRINSUB').disabled = false;
}
 }
xmlhttp.open("GET","checkdate.php?q="+str,true);
xmlhttp.send();
}
</script>

My very basic ajax code

<?php

$cdrf=trim($_GET["q"]);
list($d, $m, $y) = explode('/', $cdrf);
$mk=mktime(0, 0, 0, $m, $d, $y);
$cdrfq=strftime('%Y-%m-%d',$mk);

$q=mysql_real_escape_string($cdrfq);
$isdate = false;

$con = mysql_connect("server","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("jbsrint", $con);
$query = "SELECT * FROM fuelrecords WHERE FR_WE='$q'";
$res = mysql_query($query);
if(mysql_num_rows($res) > 0){
$response = "WARNING!! - PREVIOUS RECORDS FOUND";
}else{
$response = "NO RECORD FOUND";
}
echo $response;
?>

2 Answers 2

1

Ok I figured it out. It transpires for whatever reason (Still cant find out where from) I was getting a newline on the returned ajax code and so albeit it was only blank space the if statement could not match the phrase I had asked it to check.

By using $.trim (found in jQuery 'If' statement string comparison not working) I removed the blank space and it all fired into life.

The new code now looks like this.

if ($.trim(xmlhttp.responseText) == "WARNING!! - PREVIOUS RECORDS FOUND") document.getElementById('FRINSUB').disabled = true;
Sign up to request clarification or add additional context in comments.

Comments

-1
document.getElementById('FRINSUB').disabled = 'disabled';

2 Comments

good call with the above but I'm not struggling with the actual disabling part albeit your version I guess is more accurate. Its the if statement that controls whether or not the item is disabled that I believe is not working.
Check the request for errors. Use Firebug or something similar to check the ajax request. It should work unless PHP is throwing an uncaught error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.