【问题标题】:Using jQuery autocomplete from PHP/MySQL从 PHP/MySQL 使用 jQuery 自动完成
【发布时间】:2020-07-22 09:40:03
【问题描述】:

我的数据库列如下图:

  `matDes1` varchar(255) DEFAULT NULL,
  `matCost1` int(255) DEFAULT NULL,
  `matDes2` varchar(255) DEFAULT NULL,
  `matCost2` int(255) DEFAULT NULL,
  `matDes3` varchar(255) DEFAULT NULL,
  `matCost3` int(255) DEFAULT NULL,
  `matDes4` varchar(255) DEFAULT NULL,
  `matCost4` int(255) DEFAULT NULL,
  `matDes5` varchar(255) DEFAULT NULL,
  `matCost5` int(255) DEFAULT NULL,

我有一个动态表格,在我的 HTML 文件中添加和删除行。

在这个表格中,我有 textarea 和 input,每当我开始输入时都会向我显示建议,如果我选择一个,将自动填充其他字段。

在我的文本区域和输入下方:

<textarea id='codeANCILLARY_1' class='codeANCILLARY' name="codeANCILLARY[]"></textarea>

<input type="text" id="mat50ANCILLARY_1" class="mat50ANCILLARY" name="mat50ANCILLARY[]" />

textarea 从 matDes1 搜索到 matDes5。所以这里没问题。

问题是选择填充两个字段时,只接受MATDES1和MATCOST1而不是其余的。

有没有办法使用 if 语句之类的说:

if (matDes1 selected) {
show matDes1 with matCost1
} else if (matDes2 selected){
show matDes2 with matCost2
}
and 
so 
on

我真的不知道我该做什么了。 :(

完整的 jQuery 和 PHP 文件是:

$(document).on('keydown', '.codeANCILLARY', function () {

        var id = this.id;
        var splitid = id.split('_');
        var count = splitid[1];

        $('#' + id).autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "../../MY_PHP_PAGE",
                    type: 'post',
                    dataType: "json",
                    data: {
                        search: request.term,
                        request: 1
                    },
                    success: function (data) {
                        response(data);
                    }
                });
            },
            select: function (event, ui) {
                $(this).val(ui.item.label);
                var id = ui.item.value;

                // AJAX
                $.ajax({
                    url: '../../MY_PHP_PAGE',
                    type: 'post',
                    data: {
                        id: id,
                        request: 2
                    },
                    dataType: 'json',
                    success: function (takesAnyVaribale) {
                        var len = takesAnyVaribale.length;
                        if (len > 0) {
                            var codeANCILLARY = takesAnyVaribale[0]['codeANCILLARY'];
                            var mat50ANCILLARY = takesAnyVaribale[0]['mat50ANCILLARY'];
                            var unitsANCILLARY = takesAnyVaribale[0]['unitsANCILLARY'];
                          
                            $('#codeANCILLARY_' + count).val(codeANCILLARY);
                            $('#mat50ANCILLARY_' + count).val(mat50ANCILLARY);
                            $('#unitsANCILLARY_' + count).val(unitsANCILLARY);
                        }
                    }
                });
                return false;
            }
        });
    });
"MY_PHP_PAGE"

include "config.php";
$request = $_POST['request'];

if ($request == 1) {
    $search = $_POST['search'];
    $query1 = "SELECT * FROM MY_COMPONENTLIST WHERE matDes1 like'%".$search."%'";
    $query2 = "SELECT * FROM MY_COMPONENTLIST WHERE matDes2 like'%".$search."%'";
    $query3 = "SELECT * FROM MY_COMPONENTLIST WHERE matDes3 like'%".$search."%'";
    $query4 = "SELECT * FROM MY_COMPONENTLIST WHERE matDes4 like'%".$search."%'";
    $query5 = "SELECT * FROM MY_COMPONENTLIST WHERE matDes5 like'%".$search."%'";
    $result1 = mysqli_query($con, $query1);
    $result2 = mysqli_query($con, $query2);
    $result3 = mysqli_query($con, $query3);
    $result4 = mysqli_query($con, $query4);
    $result5 = mysqli_query($con, $query5);
    if ($result1 || $result2 || $result3 || $result4 || $result5) {
        while ($row = mysqli_fetch_array($result1)) {
            $response[] = array("value"=>$row['id'],"label"=>$row['matDes1']);
        }
        while ($row = mysqli_fetch_array($result2)) {
            $response[] = array("value"=>$row['id'],"label"=>$row['matDes2']);
        }
        while ($row = mysqli_fetch_array($result3)) {
            $response[] = array("value"=>$row['id'],"label"=>$row['matDes3']);
        }
        while ($row = mysqli_fetch_array($result4)) {
            $response[] = array("value"=>$row['id'],"label"=>$row['matDes4']);
        }
        while ($row = mysqli_fetch_array($result5)) {
            $response[] = array("value"=>$row['id'],"label"=>$row['matDes5']);
        }
    }
    echo json_encode($response);
    exit;
}

if ($request == 2) {
    $id = $_POST['id'];
    $sql = "SELECT * FROM MY_COMPONENTLIST WHERE id=".$id;
    $result = mysqli_query($con, $sql);
    $AncillaryPricing_arr = array();
    while ($row = mysqli_fetch_array($result)) {
        $id = $row['id'];
        $codeANCILLARY = $row['matDes1'];
        $mat50ANCILLARY = $row['matCost1'];
        //***************************
        //***************************
        $codeANCILLARY = $row['matDes2'];        
         $mat50ANCILLARY = $row['matCost2'];
        .and
        .so on 
        .until 5
        //***************************
        //***************************
        $unitsANCILLARY = $row['units'];
        $AncillaryPricing_arr[] = array(
            "id" => $id,
            "codeANCILLARY" => $codeANCILLARY,
            "mat50ANCILLARY" => $mat50ANCILLARY,
            "unitsANCILLARY" => $unitsANCILLARY
        );
    }
    echo json_encode($AncillaryPricing_arr);
    exit;
}

【问题讨论】:

  • 您是说您只是想通过 ID 通过在这些列中搜索来检索,或者您想检索所有这些列并将它们传回,就像您所做的那样与您已经返回的四列?如果是后者,不就是将它们全部添加到数组中吗?
  • 请记住,从这 10 列中选择的值只进入一个输入字段,而不是所有的数组
  • 如果你看 jQuery $('#codeANCILLARY_' + count).val(codeANCILLARY);

标签: php jquery mysql


【解决方案1】:

正是出于这个原因,您确实需要更好的数据库布局。将matdesmatcost 列拆分为一个单独的表,由组件ID 链接。然后,您可以拥有任意数量的内容,并且搜索它们要容易得多。而不是所有单独的查询,你会有类似的东西

SELECT * FROM MY_COMPONENTLIST_MAT WHERE matDes like'%".$search."%' and component_id = " . $id 

(除了作为准备好的语句),它会找到其中的任何一个。您可以使用 JOIN 从主组件列表中获取其余信息。

【讨论】:

  • 我可以搜索它们,但我无法选择它们
  • 我希望我能有你们中的一个在我身边并更好地解释
  • 是否可以通过 WhatsApp 或 teamviewer 与您交谈。我知道我问得太多了,但我真的卡住了请帮助pppp!!!
  • 抱歉,这两样东西我都没有。
  • 我知道我可以将我的数据库表更改为两个表,然后通过说一个 id 加入它们,但是这个表提供了我的报价系统和发票系统以及我的 PO 系统
猜你喜欢
  • 2011-07-15
  • 2012-10-18
  • 2012-06-19
  • 2019-03-07
  • 1970-01-01
  • 2017-11-11
  • 2018-10-19
  • 2018-01-08
相关资源
最近更新 更多