使用Php-ajax进行自动搜索

使用Php-ajax进行自动搜索,php,jquery,ajax,Php,Jquery,Ajax,在制作销售点系统时,我遇到了搜索问题。 当我在产品代码文本框中输入产品代码时,它会使用ajax、jquery和php自动搜索并在相关文本框中显示产品名称和价格。我试过的代码附在下面。我在运行代码时没有得到任何答案。请阅读下面的代码并尽快为我找到好的解决方案 形式设计 阿贾克斯 数据属性位于ajax配置的错误位置 这是一个修订版 将此添加到您的HTML中 <p id="output"></p> 我将您的代码粘贴到编辑器VS代码中,它给出了有用的语法错误。也许研究具有语法突

在制作销售点系统时,我遇到了搜索问题。 当我在产品代码文本框中输入产品代码时,它会使用ajax、jquery和php自动搜索并在相关文本框中显示产品名称和价格。我试过的代码附在下面。我在运行代码时没有得到任何答案。请阅读下面的代码并尽快为我找到好的解决方案

形式设计

阿贾克斯


数据属性位于ajax配置的错误位置

这是一个修订版

将此添加到您的HTML中

<p id="output"></p>

我将您的代码粘贴到编辑器VS代码中,它给出了有用的语法错误。也许研究具有语法突出显示和检查功能的编辑器是个好主意?

您尝试过调试什么?查询运行正常吗?调试get_purchase.php以查看数据是否正确聚合并写入数组,或者在frontendyes中显示该数组的结果时是否有问题,这对您来说应该不是太困难。我检查了sir数据库查询是否正常运行。我检查了mysql。我对ajex传递值有问题,你能检查sir吗?那么通过PHP生成的结果肯定是可以的?是的,先生。我正在等待结果。据我所知,你的JS代码已经坏了。为什么成功函数中有数据键?未捕获引用错误:未定义showModal。我犯了个错误。先生,我需要的是,当我在Product Code(产品代码)文本框中输入产品代码时,它会自动搜索并在相关文本框中显示产品名称和价格。此修订代码中未使用ShowModal(显示模式)以及您显示的代码。似乎在未定义showModal函数的情况下调用了该函数。请检查此函数的代码并确保其已定义。先生,我已在上面编辑了您的代码。不是组合框。文本框。我已经把文本框的名称和forloop中的结果放在文本框上。当我在“产品代码”文本框中输入产品代码时,它会自动搜索并将产品名称和价格显示在相关的窗口中,这意味着它几乎可以工作了!只需在html中创建另一个元素即可。在编写ajex代码时,使用$output将s附加到而不是$product_codecan。没有组合框,先生。当我在Product Code(产品代码)文本框中输入产品代码时,它会自动搜索并显示只有我需要的相关产品名称和价格。使用$product\U code.KEYUPFUNCTION
    function getProductcode() {
        $('#product_code').empty();

        $("#product_code").keyup(function(e) {
                var q = $("#product_code").val();


                $.ajax({
                        type: 'POST',
                        url: '../php/project_module/get_purchase.php',
                        dataType: 'JSON',
                        async: false,
                        success: function(data) {

                            for (var i = 0; i < data.length; i++) {
                                $('#product_code').append($("<option/>", {
                                    value: data[i].product_id,
                                    text: data[i].product_name,


                                }));
                            }
                            data: {
                                    product_code: $('#product_code').val()
                                },

                        }
                    },

                    error: function(xhr, status, error) {

                        alert(xhr.responseText);
                        //
                    }

                });
        }

**get_purchase.php**

    $product_code = $_POST["product_code"];
    $stmt = $conn->prepare("select  product_id,product_name,barcode,category,description,warrenty,product_condition,price_retail,
                       price_cost,discount,reorderlevel,brand
         from product  where barcode = ?");

    $stmt->bind_param("s", $product_code);
    $stmt->bind_result($product_id,$product_name,$price);

    if ($stmt->execute()) {
        while ( $stmt->fetch() ) {
            $output[] = array ("product_id"=>$product_id,"product_name"=>$product_name,"price_retail"=>$price);
        }
        echo json_encode( $output );
    }
    $stmt->close();
<p id="output"></p>
function getProductcode() {
  $("#product_code").empty();

  $("#product_code").keyup(function(e) {
    var q = $("#product_code").val();

    $.ajax({
      type: "POST",
      url: "../php/project_module/get_purchase.php",
      dataType: "JSON",
      data: { product_code: $("#product_code").val() },
      success: function(data) {
        if(typeof data === 'undefined'){
          return
        }
        $("#output").val(data[0].product_id + " : "  +data[0].product_name)
      },
      error: function(xhr, status, error) {
        alert(xhr.responseText);
      }
    });
  });
}