gpt4 book ai didi

javascript - ajax请求,查询数据库,json_encode,成功返回,显示结果

转载 作者:行者123 更新时间:2023-11-30 08:22:21 25 4
gpt4 key购买 nike

我正在编写一段新代码,我是 ajax 的新手,所以我无法让它工作。我有一个文本框,其中分配了一个 javascript onKeyUp。第一个函数是延迟函数。它设置了一个延迟计时器,只要没有通过该延迟计时器发出其他请求,它就会在一段时间后使用 ajax 运行第二个函数。在 ajax 内部,它根据在文本框中输入的文本运行位于第二个文件中的数据库查询。它设置一个结果数组,对它们进行 json_encodes,然后将它们返回到原始页面。然后,我需要用结果填充页面(使用 php)。

文本框

<input type="text" onKeyUp="delay_method('search_customer',search_customer(this.value),'2000')" />

延时函数

<script type="text/javascript">
function delay_method(label,callback,time){
if(typeof window.delayed_methods=="undefined"){window.delayed_methods={};}
delayed_methods[label]=Date.now();
var t=delayed_methods[label];
setTimeout(function(){ if(delayed_methods[label]!=t){return;}else{ callback();}}, time||500);
}
</script>

ajax函数

<script type="text/javascript">

function search_customer(value){

console.log(value);

$.ajax({
type: "POST",
url: "secondpage.php",
data: query,
dataType: 'json',
success: function(data){
console.log(data.name); // customer name
console.log(data.company); // customer company name
}
});
}

</script>

第二页

设置一个数组进行测试。现在绕过查询。只需要将结果返回到主页。一旦它工作,我可以从 php 设置数组。我的查询将使用 LIKE %search text%

$arr = array(
'name'=>'overflow',
'company'=>'value'
);
echo json_encode($arr);

我不知道如何从 ajax 函数检索数据并填充结果。我很想将结果返回到一个 php 数组中,然后循环遍历该数组以回显结果。我可以在 php 中遍历数组...我最关心的是将结果返回到 php 数组中。

任何帮助都会很棒。我似乎无法让代码工作。我是 ajax 的新手,所以我边学边学。





更新

除延迟外,一切正常。它不会拖延任何事情。在激活 ajax 函数之前,我需要它从每个 keyup 等待 2 秒。如果它收到另一个 keyup,它会再等待 2 秒。 IT 一直持续到 2 秒没有按键。这样一来,如果该人仍在打字,它就不会在每次键入时都查询数据库。现在它处理每个 keyup 的所有内容。

文本框

<input type="text" onKeyUp="delay_method('search_customer',search_customer(this.value),'2000')" />

延迟

function delay_method(label,callback,time){
if(typeof window.delayed_methods=="undefined"){window.delayed_methods={};}
delayed_methods[label]=Date.now();
var t=delayed_methods[label];
setTimeout(function(){ if(delayed_methods[label]!=t){return;}else{ callback();}}, time||500);
}

ajax函数

function search_customer(value){    

console.log(value);

$.ajax({
type: "POST",
url: "secondpage.php",
data: { "value": value },
dataType: 'html',
success: function(data){
$('#resultDiv').html(data);
}
});
}

第二页:

$value = $_POST['value'];

if ((isset($value)) && ($value != "")) {
$arr = array();
$search_query = $db1q->query("SELECT first_name, company FROM Users WHERE first_name LIKE '%$value%' OR last_name LIKE '%$value%' OR company LIKE '%$value%'");
if ($search_query->num_rows > 0) {

while ($search_result = $search_query->fetch_assoc()) {
$arr[$search_result['first_name']] = $search_result['company'];
}
}

$html = '';
$html .= '<div>';
foreach ($arr as $key => $value) {
$html .= '<div class="sub-div"><h2>'.$key.'</h2> : ' . '<h4>' . $value . '</h4></div>';
}
$html .= '</div>';

echo $html;
}

最佳答案

你无法将结果返回到 js 中的 php 数组中。所以,你要做的就是将处理后的 html 传递到 js 中,然后在页面上打印。

例如,在第二页

$arr = array(
'name'=>'overflow',
'company'=>'value'
);

使用上面的数组 $arr 在此处生成 html 并将其存储在变量中。将该变量传递给 js。

例如:

    $html = '';
$html .= '<div>';
foreach ($arr as $key => $value) {
$html .= '<div class="sub-div"><h2>'.$key.'</h2> : ' . '<h4>' . $value . '</h4></div>';
}
$html .= '</div>';

echo $html;

现在您将在 ajax 成功中获得 html。就像显示在 div 上一样

$('resultDiv').html(data);

关于javascript - ajax请求,查询数据库,json_encode,成功返回,显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51393779/

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