gpt4 book ai didi

php - 如何在ajax中传递多个#div id以实现高级搜索功能

转载 作者:行者123 更新时间:2023-12-01 04:02:02 26 4
gpt4 key购买 nike

index.php

$('#search1, #search2').change(function() {
var value = $(this).val();
$.ajax({
type:"post",
url:"php/search.php",
data:'task1='+value,
success: function(response){
$('#result').html(response);
}
});
});

<select id="search1" name="task1">....</select>
<select id="search2" name="task2">...</select>

<div id="result"></div>

search.php

if (isset($_POST['task1']) ? $_POST['task1'] : false) {
$option = isset($_POST['task1']) ? $_POST['task1'] : false;
$sql = "SELECT * FROM <tablename> WHERE <columnName>='$option'";
if (isset($_POST['task2']) ? $_POST['task2'] : false) {
$pilihan = isset($_POST['task2']) ? $_POST['task2'] : false;
$sql = "SELECT * FROM <tablename> WHERE <columnName>='$option' and <columnName>='$pilihan'";
}
}

我的问题是如何将多个 #div 传递到一个 ajax 中。换句话说,如何确保#search2和#task2能够像#search1和#task1一样传递和查询其进程。

最佳答案

You can get the name of select tag while change the options and make new variable, then pass that too data.

index.php

$('#search1, #search2').change(function() {
var value1 = $(#search1).val();
var value2 = $(#search2).val();
var str = 'task1='+value1+'task2='+value2;
$.ajax({
type:"GET",
url:"php/search.php",
data: str,
success: function(response){
$('#result').html(response);
}
});
});

<select id="search1" name="task1">....</select>
<select id="search2" name="task2">...</select>

<div id="result"></div>

search.php

$option = false;
if (isset($_GET['task1'])) {
$option = $_GET['task1'];
$sql = "SELECT * FROM <tablename> WHERE <columnName>='$option'";
}
if (isset($_GET['task2']) {
if (isset($_GET['task1'])) {
$option = $_GET['task1'];
}
$pilihan = $_GET['task2'];
$sql = "SELECT * FROM <tablename> WHERE <columnName>='$option' and <columnName>='$pilihan'";
}

我希望这将有助于实现您的目标

关于php - 如何在ajax中传递多个#div id以实现高级搜索功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39543386/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com