简体   繁体   中英

Enabling/Disabling a button after database query

I want to disable or enable a button, depending on the result of a database-query. But I don't know how. From an example, I managed to show a text (id="error", depending on the result of the query, but enabling the button (id="generate") does not work.

This is my JavaScript:

function checkSender(str)
{
    if(str == "")
    {
        str=document.getElementById("senderinput").value;
    }
    str=str.toUpperCase();
    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("error").innerHTML=xmlhttp.responseText;
            if(xmlhttp.responseText == "Einsender existiert nicht.")
            {
                document.getElementById("generate").disabled = true;
            }
            else
            {
                document.getElementById("generate").disabled = false;
            }
        }
    }
    xmlhttp.open("GET","checkSender.php?s="+str,true);
    xmlhttp.send();
}

The response from checkSender.php is either "Einsender existiert nicht." or an empty string.

Any suggestions?

EDIT :

The PHP-Code:

<?php
require 'classes/DBHandler.php';
$DBHandler = new class_DBHandler();
$s = $_GET['s'];

$query="<Statement with $s as parameter>";

$Data = $DBHandler->GetData($query);
if(intval($Data[0]['COUNT']) >= 1)
{
    echo "";
}
else
{
    echo "Einsender existiert nicht.";
}
?> 

The HTML-Code:

<!DOCTYPE html>
<html>
  <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
      <link rel="stylesheet" type="text/css" href="style.css">
      <script src="inc/checkSender.js"></script>
      <title>MarPro</title>
  </head>
  <body>
      <div class="main">
          <div class="debug" id="ana">
          </div>
          <div class="headline">
              <h1>MarPro</h1>
          </div>
          <div class="result">
              <div class="menu">
                  <form action="" method="post" name="senderform">
                      <p>Einsender: <input type="text" name="sender" onkeyup="checkSender(this.value)"></p>
                      <p id="error"></p>
                      <p><input type="submit" name="generate" value="Generieren" id="generate" disabled></p>
                  </form>
              </div>
          </div>
      </div>
  </body>
</html>

Thanks in advance!

Marco Frost

I think you should try using alert to see what your "responseText" looks like. It might not be both of the expected values because if it were then you would have got desired results.

I've found the solution.

The problem was that "echo" added a whitespace and a linebreak after printing the string. So I had to manipulate the string like this before checking it:

String.trim(xmlhttp.responseText)

Now, it works.

if(String.trim(xmlhttp.responseText) === "Einsender existiert nicht.")
{
    foo();
}
else
{
    bar();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM