依赖下拉多个 .onchange
Dependent drop down with multiple .onchange
我正在开发一个搜索系统,系统会在 index.php
上提示用户 4 个下拉列表。我让用户选择第一个下拉菜单,然后第二个、第三个和第四个下拉菜单的值更改为与第一个下拉菜单相关的值。我正在使用 _POST
获取 onchange
值并将数据显示到下拉列表的其余部分。我也做了同样的事情,如果用户在初始搜索中选择了第二个下拉菜单,那么第一个、第三个和第四个值将更改为与所选下拉菜单相关的值。我已经为所有下拉列表完成了此操作。该系统正在运行,但是有没有比使用多个 _POST
和 .onchange
值更好的方法呢?
这是下拉脚本:
$(document).ready(function(){
/////start commodity post
$('#pfcommodity').on('change', function(){
var pfcommodity = $(this).val();
if(pfcommodity){
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfcategory",
data:'pfcommodity='+pfcommodity,
success: function(data)
{
//console.log(data);
$("#pfcategory").html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pforigin",
data:'pfcommodity='+pfcommodity,
success: function(data)
{
$("#pforigin").html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfmanufacture",
data:'pfcommodity='+pfcommodity,
success: function(data)
{
$("#pfmanufacture").html(data);
}
});
}
});////close commodity post
////open category post
$('#pfcategory').on('change', function(){
var pfcategory = $(this).val();
if(pfcategory){
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfcategory",
data:'pfcategory='+pfcategory,
success: function(data)
{
//console.log(data);
$("#pfcommodity").html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pforigin",
data:'pfcategory='+pfcategory,
success: function(data)
{
$("#pforigin").html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfmanufacture",
data:'pfcategory='+pfcategory,
success: function(data)
{
$("#pfmanufacture").html(data);
}
});
}
});////close category post
// ////open origin post
$('#pforigin').on('change', function(){
var pforigin = $(this).val();
if(pforigin){
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfcategory",
data:'pforigin='+pforigin,
success: function(data)
{
//console.log(data);
$("#pfcommodity").html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pforigin",
data:'pforigin='+pforigin,
success: function(data)
{
$("#pfcategory").html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfmanufacture",
data:'pforigin='+pforigin,
success: function(data)
{
$("#pfmanufacture").html(data);
}
});
}
});////close origin post
// ////open manufacure post
$('#pfmanufacture').on('change', function(){
var pfmanufacture = $(this).val();
if(pfmanufacture){
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfcategory",
data:'pfmanufacture='+pfmanufacture,
success: function(data)
{
//console.log(data);
$("#pfcommodity").html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pforigin",
data:'pfmanufacture='+pfmanufacture,
success: function(data)
{
$("#pfcategory").html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfmanufacture",
data:'pfmanufacture='+pfmanufacture,
success: function(data)
{
$("#pforigin").html(data);
}
});
}
});////close manufacture post
});
这是第一个查询pfcategory.php
<?php
if (isset($_POST['pfcommodity'])){
$connect=mysqli_connect("localhost","root","","import-conditions");
$output='';
$sql="SELECT DISTINCT category FROM processfood WHERE commodity = '".$_POST["pfcommodity"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Category</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["category"].'</option>';
}
echo $output;
}
if (isset($_POST['pfcategory'])){
$connect=mysqli_connect("localhost","root","","import-conditions");
$output='';
$sql="SELECT DISTINCT commodity FROM processfood WHERE category = '".$_POST["pfcategory"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Commodity</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["commodity"].'</option>';
}
echo $output;
}
if (isset($_POST['pforigin'])){
$connect=mysqli_connect("localhost","root","","import-conditions");
$output='';
$sql="SELECT DISTINCT commodity FROM processfood WHERE origin = '".$_POST["pforigin"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Commodity</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["commodity"].'</option>';
}
echo $output;
}
if (isset($_POST['pfmanufacture'])){
$connect=mysqli_connect("localhost","root","","import-conditions");
$output='';
$sql="SELECT DISTINCT commodity FROM processfood WHERE manufacture = '".$_POST["pfmanufacture"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Commodity</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["commodity"].'</option>';
}
echo $output;
}
?>
这是pforigin.php
<?php
$connect=mysqli_connect("localhost","root","","import-conditions");
$output='';
if (isset($_POST['pfcommodity'])){
$sql="SELECT DISTINCT origin FROM processfood WHERE commodity = '".$_POST["pfcommodity"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Country of Origin</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["origin"].'</option>';
}
echo $output;
}
if (isset($_POST['pfcategory'])){
$sql="SELECT DISTINCT origin FROM processfood WHERE category = '".$_POST["pfcategory"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Country of Origin</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["origin"].'</option>';
}
echo $output;
}
if (isset($_POST['pforigin'])){
$sql="SELECT DISTINCT category FROM processfood WHERE origin = '".$_POST["pforigin"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Category</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["category"].'</option>';
}
echo $output;
}
if (isset($_POST['pfmanufacture'])){
$sql="SELECT DISTINCT category FROM processfood WHERE manufacture = '".$_POST["pfmanufacture"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Category</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["category"].'</option>';
}
echo $output;
}
?>
这是pfmanufacture.php
<?php
$connect=mysqli_connect("localhost","root","","import-conditions");
$output='';
if (isset($_POST['pfcommodity'])){
$sql="SELECT DISTINCT manufacture FROM processfood WHERE commodity = '".$_POST["pfcommodity"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Manufacture</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["manufacture"].'</option>';
}
echo $output;
}
if (isset($_POST['pfcategory'])){
$sql="SELECT DISTINCT manufacture FROM processfood WHERE category = '".$_POST["pfcategory"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Manufacture</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["manufacture"].'</option>';
}
echo $output;
}
if (isset($_POST['pforigin'])){
$sql="SELECT DISTINCT manufacture FROM processfood WHERE origin = '".$_POST["pforigin"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Manufacture</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["manufacture"].'</option>';
}
echo $output;
}
if (isset($_POST['pfmanufacture'])){
$sql="SELECT DISTINCT origin FROM processfood WHERE manufacture = '".$_POST["pfmanufacture"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Country of Origin</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["origin"].'</option>';
}
echo $output;
}
?>
所有其他页面都遵循相同的模式。到目前为止没有问题,但是有更好的方法吗?
首先通过使用事件处理程序class
减少jquery代码,使用数据类型属性传入类型。
<select class="pf_select" data-type="commodity">
<option value="">Select Category</option>
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
<select>
这样做你可以删除除其中一个 jquery 调用之外的所有调用,只需稍作改动。
$('.pf_select').on('change', function() {
var elm = $(this);
var type = elm.data('type');
var commodity = elm.val();
if (commodity !== '') {
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfcategory",
data: 'pf' + type + '=' + commodity,
success: function(data) {
elm.html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pforigin",
data: 'pf' + type + '=' + commodity,
success: function(data) {
elm.html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfmanufacture",
data: 'pf' + type + '=' + commodity,
success: function(data) {
elm.html(data);
}
});
}
});
现在进入 PHP:
您只需要连接到数据库一次,所以只需将其放在顶部即可。
然后调用之间发生的所有变化就是 table 名称和 POST 变量键。因此,您可以轻松地将数据库调用和 html 渲染包装到一个函数中并传入变量。
然后您只需在 if 语句中调用该函数即可。
<?php
$connect = mysqli_connect("localhost","root","","import-conditions");
function pf_select($key, $select_label = 'Select Category') {
global $connect;
$stmt = $connect->prepare('
SELECT DISTINCT category
FROM processfood
WHERE '.$key.' = ?');
$stmt->bind_param('s', $_POST['pf'.$key]);
$stmt->execute();
$result = $stmt->get_result();
$output = '<option value="">'.$select_label.'</option>';
while ($row = $result->fetch_assoc()) {
$output .= '<option value="'.$row["category"].'">'.$row["category"].'</option>';
}
return $output;
}
if (isset($_POST['pfcommodity'])) {
exit(pf_select('commodity', 'Select Category'));
}
if (isset($_POST['pfcategory'])) {
exit(pf_select('category', 'Select Commodity'));
}
if (isset($_POST['pforigin'])) {
exit(pf_select('origin', 'Select Commodity'));
}
if (isset($_POST['pfmanufacture'])) {
exit(pf_select('manufacture', 'Select Commodity'));
}
更具可读性和可管理性。
注意:我没有添加数据库错误检查,无论如何你应该添加它,并阅读防止 SQL 注入。
我正在开发一个搜索系统,系统会在 index.php
上提示用户 4 个下拉列表。我让用户选择第一个下拉菜单,然后第二个、第三个和第四个下拉菜单的值更改为与第一个下拉菜单相关的值。我正在使用 _POST
获取 onchange
值并将数据显示到下拉列表的其余部分。我也做了同样的事情,如果用户在初始搜索中选择了第二个下拉菜单,那么第一个、第三个和第四个值将更改为与所选下拉菜单相关的值。我已经为所有下拉列表完成了此操作。该系统正在运行,但是有没有比使用多个 _POST
和 .onchange
值更好的方法呢?
这是下拉脚本:
$(document).ready(function(){
/////start commodity post
$('#pfcommodity').on('change', function(){
var pfcommodity = $(this).val();
if(pfcommodity){
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfcategory",
data:'pfcommodity='+pfcommodity,
success: function(data)
{
//console.log(data);
$("#pfcategory").html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pforigin",
data:'pfcommodity='+pfcommodity,
success: function(data)
{
$("#pforigin").html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfmanufacture",
data:'pfcommodity='+pfcommodity,
success: function(data)
{
$("#pfmanufacture").html(data);
}
});
}
});////close commodity post
////open category post
$('#pfcategory').on('change', function(){
var pfcategory = $(this).val();
if(pfcategory){
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfcategory",
data:'pfcategory='+pfcategory,
success: function(data)
{
//console.log(data);
$("#pfcommodity").html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pforigin",
data:'pfcategory='+pfcategory,
success: function(data)
{
$("#pforigin").html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfmanufacture",
data:'pfcategory='+pfcategory,
success: function(data)
{
$("#pfmanufacture").html(data);
}
});
}
});////close category post
// ////open origin post
$('#pforigin').on('change', function(){
var pforigin = $(this).val();
if(pforigin){
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfcategory",
data:'pforigin='+pforigin,
success: function(data)
{
//console.log(data);
$("#pfcommodity").html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pforigin",
data:'pforigin='+pforigin,
success: function(data)
{
$("#pfcategory").html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfmanufacture",
data:'pforigin='+pforigin,
success: function(data)
{
$("#pfmanufacture").html(data);
}
});
}
});////close origin post
// ////open manufacure post
$('#pfmanufacture').on('change', function(){
var pfmanufacture = $(this).val();
if(pfmanufacture){
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfcategory",
data:'pfmanufacture='+pfmanufacture,
success: function(data)
{
//console.log(data);
$("#pfcommodity").html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pforigin",
data:'pfmanufacture='+pfmanufacture,
success: function(data)
{
$("#pfcategory").html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfmanufacture",
data:'pfmanufacture='+pfmanufacture,
success: function(data)
{
$("#pforigin").html(data);
}
});
}
});////close manufacture post
});
这是第一个查询pfcategory.php
<?php
if (isset($_POST['pfcommodity'])){
$connect=mysqli_connect("localhost","root","","import-conditions");
$output='';
$sql="SELECT DISTINCT category FROM processfood WHERE commodity = '".$_POST["pfcommodity"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Category</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["category"].'</option>';
}
echo $output;
}
if (isset($_POST['pfcategory'])){
$connect=mysqli_connect("localhost","root","","import-conditions");
$output='';
$sql="SELECT DISTINCT commodity FROM processfood WHERE category = '".$_POST["pfcategory"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Commodity</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["commodity"].'</option>';
}
echo $output;
}
if (isset($_POST['pforigin'])){
$connect=mysqli_connect("localhost","root","","import-conditions");
$output='';
$sql="SELECT DISTINCT commodity FROM processfood WHERE origin = '".$_POST["pforigin"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Commodity</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["commodity"].'</option>';
}
echo $output;
}
if (isset($_POST['pfmanufacture'])){
$connect=mysqli_connect("localhost","root","","import-conditions");
$output='';
$sql="SELECT DISTINCT commodity FROM processfood WHERE manufacture = '".$_POST["pfmanufacture"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Commodity</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["commodity"].'</option>';
}
echo $output;
}
?>
这是pforigin.php
<?php
$connect=mysqli_connect("localhost","root","","import-conditions");
$output='';
if (isset($_POST['pfcommodity'])){
$sql="SELECT DISTINCT origin FROM processfood WHERE commodity = '".$_POST["pfcommodity"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Country of Origin</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["origin"].'</option>';
}
echo $output;
}
if (isset($_POST['pfcategory'])){
$sql="SELECT DISTINCT origin FROM processfood WHERE category = '".$_POST["pfcategory"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Country of Origin</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["origin"].'</option>';
}
echo $output;
}
if (isset($_POST['pforigin'])){
$sql="SELECT DISTINCT category FROM processfood WHERE origin = '".$_POST["pforigin"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Category</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["category"].'</option>';
}
echo $output;
}
if (isset($_POST['pfmanufacture'])){
$sql="SELECT DISTINCT category FROM processfood WHERE manufacture = '".$_POST["pfmanufacture"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Category</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["category"].'</option>';
}
echo $output;
}
?>
这是pfmanufacture.php
<?php
$connect=mysqli_connect("localhost","root","","import-conditions");
$output='';
if (isset($_POST['pfcommodity'])){
$sql="SELECT DISTINCT manufacture FROM processfood WHERE commodity = '".$_POST["pfcommodity"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Manufacture</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["manufacture"].'</option>';
}
echo $output;
}
if (isset($_POST['pfcategory'])){
$sql="SELECT DISTINCT manufacture FROM processfood WHERE category = '".$_POST["pfcategory"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Manufacture</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["manufacture"].'</option>';
}
echo $output;
}
if (isset($_POST['pforigin'])){
$sql="SELECT DISTINCT manufacture FROM processfood WHERE origin = '".$_POST["pforigin"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Manufacture</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["manufacture"].'</option>';
}
echo $output;
}
if (isset($_POST['pfmanufacture'])){
$sql="SELECT DISTINCT origin FROM processfood WHERE manufacture = '".$_POST["pfmanufacture"]."'";
$result=mysqli_query($connect,$sql);
$output.='<option value="">Select Country of Origin</option>';
while ($row=mysqli_fetch_array($result)) {
$output.='<option value="">'.$row["origin"].'</option>';
}
echo $output;
}
?>
所有其他页面都遵循相同的模式。到目前为止没有问题,但是有更好的方法吗?
首先通过使用事件处理程序class
减少jquery代码,使用数据类型属性传入类型。
<select class="pf_select" data-type="commodity">
<option value="">Select Category</option>
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
<select>
这样做你可以删除除其中一个 jquery 调用之外的所有调用,只需稍作改动。
$('.pf_select').on('change', function() {
var elm = $(this);
var type = elm.data('type');
var commodity = elm.val();
if (commodity !== '') {
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfcategory",
data: 'pf' + type + '=' + commodity,
success: function(data) {
elm.html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pforigin",
data: 'pf' + type + '=' + commodity,
success: function(data) {
elm.html(data);
}
});
$.ajax({
method: "POST",
url: "/import-conditions/processfood/pfmanufacture",
data: 'pf' + type + '=' + commodity,
success: function(data) {
elm.html(data);
}
});
}
});
现在进入 PHP:
您只需要连接到数据库一次,所以只需将其放在顶部即可。
然后调用之间发生的所有变化就是 table 名称和 POST 变量键。因此,您可以轻松地将数据库调用和 html 渲染包装到一个函数中并传入变量。
然后您只需在 if 语句中调用该函数即可。
<?php
$connect = mysqli_connect("localhost","root","","import-conditions");
function pf_select($key, $select_label = 'Select Category') {
global $connect;
$stmt = $connect->prepare('
SELECT DISTINCT category
FROM processfood
WHERE '.$key.' = ?');
$stmt->bind_param('s', $_POST['pf'.$key]);
$stmt->execute();
$result = $stmt->get_result();
$output = '<option value="">'.$select_label.'</option>';
while ($row = $result->fetch_assoc()) {
$output .= '<option value="'.$row["category"].'">'.$row["category"].'</option>';
}
return $output;
}
if (isset($_POST['pfcommodity'])) {
exit(pf_select('commodity', 'Select Category'));
}
if (isset($_POST['pfcategory'])) {
exit(pf_select('category', 'Select Commodity'));
}
if (isset($_POST['pforigin'])) {
exit(pf_select('origin', 'Select Commodity'));
}
if (isset($_POST['pfmanufacture'])) {
exit(pf_select('manufacture', 'Select Commodity'));
}
更具可读性和可管理性。
注意:我没有添加数据库错误检查,无论如何你应该添加它,并阅读防止 SQL 注入。