繁体   English   中英

从 PHP/MySQL 使用 jQuery 自动完成

[英]Using jQuery autocomplete from PHP/MySQL

我的数据库列如下图:

  `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,每当我开始输入时都会向我显示建议,如果我 select 之一,将自动填充其他字段。

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

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

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

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

问题是选择两个字段时,仅接受MATDES1和MATCOST1而不是Z65E8800B5C6800AAD896F8888888B2A62AFCZ。

有没有办法使用 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; }

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM