Disable Submit Button Based on Ajax response

余生颓废 提交于 2020-01-11 13:19:07

问题


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;
?>

回答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;



回答2:


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


来源:https://stackoverflow.com/questions/9445599/disable-submit-button-based-on-ajax-response

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!