如何在 if 语句中使用输入字段的值


How to use the value of input fields in an if statement

我有两个输入字段。一种是使用 jquery.autocomplete 搜索自动建议产品名称,在我的数据库中我有列名product left,我想在第二个输入中显示它,这可能吗?

例如:我有一个名为apple的产品,product left 30apple将在键入后显示在第一个输入字段中,同时30必须出现在second field中。

<form action="sales.php" method="post">
    <input name="productlist" type="text" id="productlist" size="20"/>
    <input name="productleft" type="text" value=""/>
</form>

调用 jquery UI

<script type="text/javascript" src="js/jquery.autocomplete.js"></script>
<script>
  $(document).ready(function(){
   $("#productlist").autocomplete("psuggest.php", {
        selectFirst: true
    });
  });
  </script>

在建议里面.php

<?php
    $q=$_GET['q'];
    $my_data=mysql_real_escape_string($q);
    $mysqli=mysqli_connect('localhost','root','','saganatracker') or die("Database Error");
    $sql="SELECT pdesc FROM products WHERE pdesc LIKE '%$my_data%' ORDER BY pdesc";
    $result = mysqli_query($mysqli,$sql) or die(mysqli_error());
    if($result)
    {
        while($row=mysqli_fetch_array($result))
        {
            echo $row['pdesc']."'n";
        }
    }
?>

使用参数 onselect.autocomplete() 带来剩余的项目数:

$(document).ready(function(){
    $("#productlist").autocomplete("psuggest.php", {
        selectFirst : true,
        onselect    : function(value, data) {
            $.post("myPhPfileWhichBringNumberOfItem.php", { product : value }, function(count) {
                $("#productleft").val(count); // Don't forget to mention an id for the input first
            }
        }
    });
});

在选择值的那一刻使用 JQuery 帖子,然后使用打开数据库的 php 脚本,转到表并选择剩余的产品数量(不确定您应该使用 value 还是data,如果我错了,请有人纠正)。