Disable Submit Button Based on Ajax response

2019-03-03 10:04发布

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条回答
再贱就再见
2楼-- · 2019-03-03 10:29

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;
查看更多
老娘就宠你
3楼-- · 2019-03-03 10:32
document.getElementById('FRINSUB').disabled = 'disabled';
查看更多
登录 后发表回答