根据输入数据显示条件为真的按钮

时间:2018-02-12 11:00:04

标签: javascript php jquery html mysql

我有一个输入文本字段,我使用javascript和php从数据库中获取数据。 get.html 通过javascript获取数据

    function showUser(str) {
        if (str == "") {
            document.getElementById("txtHint").innerHTML = "";
            return;
        } else {
            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 (this.readyState == 4 && this.status == 200) {
                    document.getElementById("txtHint").innerHTML = this.responseText;
                }
            };
            xmlhttp.open("GET","get.php?q="+str,true);
            xmlhttp.send();
        }
    }

<input type="text" id="name" onblur="showUser(this.value)">
<input type="buton" name="">
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>

get.php 连接数据库以显示数据

<?php
$q = intval($_GET['q']);

$con=mysql_connect("localhost","root","") or die("Could not Connect with Sql");
mysql_select_db("dialog",$con)  or die("Could connect to Database");
$sql="SELECT * FROM customer WHERE cx_number = '".$q."'";
$result = mysql_query($sql);

echo "<table>
<tr>
<th>cx_number</th>
<th>package</th>
<th>balance</th>
</tr>";
while($row = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['cx_number'] . "</td>";
    echo "<td>" . $row['package'] . "</td>";
    echo "<td>" . $row['balance'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>

现在我的问题是如果包裹余额为零,如何显示按钮以增加包裹数量

2 个答案:

答案 0 :(得分:3)

如果您需要为每个用户提供,只需在get.php中替换

action_time

echo "<td>" . $row['balance'] . "</td>";

您也可以在浏览器端执行此操作。

之后添加
echo "<td>" . $row['balance'] ; 
if ($row['balance'] <= 0) echo "<button>Your button </button>";
echo "</td>";

像这样的代码

document.getElementById("txtHint").innerHTML = this.responseText;

答案 1 :(得分:1)

你可以在你的div中更改它:

function showUser(str) {
        if (str == "") {
            document.getElementById("txtHint").innerHTML = "";
            return;
        } else {
            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 (this.readyState == 4 && this.status == 200) {
                    document.getElementById("txtHint").innerHTML = this.responseText;
                }
            };
            xmlhttp.open("GET","get.php?q="+str,true);
            xmlhttp.send();
        }
    }

<input type="text" id="name" onblur="showUser(this.value)">
<div id="txtHint">
<b>Person info will be listed here...</b></div>
你的ajax文件中的

 <?php
$q = intval($_GET['q']);

$con=mysql_connect("localhost","root","") or die("Could not Connect with Sql");
mysql_select_db("dialog",$con)  or die("Could connect to Database");
$sql="SELECT * FROM customer WHERE cx_number = '".$q."'";
$result = mysql_query($sql);

echo "<table>
<tr>
<th>cx_number</th>
<th>package</th>
<th>balance</th>
</tr>";
while($row = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['cx_number'] . "</td>";
    echo "<td>" . $row['package'] . "</td>";
    echo "<td>" . $row['balance'] . "</td>";
    echo "<td>". if( $row['balance'] == 0) {  '<input type="button" name="" value="your value"> } '. "</td>";
    echo "</tr>";
}
echo "</table>";

mysql_close($con);
?>