简体   繁体   English

数据库查询后启用/禁用按钮

[英]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. 从一个例子中,我设法显示一个文本(id =“error”,取决于查询的结果,但启用按钮(id =“generate”)不起作用。

This is my JavaScript: 这是我的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." 来自checkSender.php的回复是“Einsender existiert nicht”。 or an empty string. 或一个空字符串。

Any suggestions? 有什么建议么?

EDIT : 编辑

The PHP-Code: PHP代码:

<?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: HTML代码:

<!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. 我认为您应该尝试使用警报来查看“responseText”的样子。 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. 问题是“echo”在打印字符串后添加了空格和换行符。 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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