doulutian4843 2015-01-28 07:55
浏览 80

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

I have two input fields. One is to search auto suggest product names using jquery.autocomplete, and in my database I have column name product left, and I want to display it in the second input, is that possible?

For example: I have a product called apple with product left of 30. apple will display in the first input field after typing, and at same time 30 must appear in the second field.

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

call jquery ui

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

inside psuggest.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']."
";
        }
    }
?>
  • 写回答

1条回答 默认 最新

  • dtp87205 2015-01-28 08:08
    关注

    Use parameter onselect from .autocomplete() to bring the number of item left :

    $(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
                }
            }
        });
    });
    

    Use JQuery post at the moment a value is selected, and then use a php script which open your database, go to the table and select number of product left (not sure if you should use value or data, please someone correct if I am wrong).

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮