gpt4 book ai didi

javascript - 在同一页面的 div 中显示 ajax 调用结果

转载 作者:行者123 更新时间:2023-12-01 03:38:15 24 4
gpt4 key购买 nike

我尝试了一些代码来在同一页面上获取结果。但它不起作用。它在 result.php 文件中向我显示结果。卡住了 ot。我对 AJAX 完全陌生。如有任何建议,请。

AJAX

  $("document").ready(function(){
$(".but").submit(function(){
event.preventDefault();
var $form = $( this ),
var myData=$form.find( 'input[name="query"]' ).val(),
$.ajax({
url: 'result.php',
data: myData,
type: 'post',
dataType: "html",
success: function(result){
$('.coupons').text(result)
} }); }); });

结果.php

$keyword = mysqli_real_escape_string($con,$_GET['query']); // always escape
$keys = explode(" ", $keyword);
$sql="SELECT c.* , sc.* , sm.* ,ca.* from store_category sc INNER JOIN store_manufacture sm ON sm.sm_id=sc.store_id INNER JOIN categories ca ON ca.cat_id=sc.cat_id INNER JOIN coupons c on c.c_sc_id=sc.sc_id WHERE c.c_name LIKE '%$keyword%' OR sm.sm_brand_name LIKE '%$keyword%' OR ca.cat_name LIKE '%$keyword%' OR c.c_description LIKE '%$keyword%'";

foreach ($keys as $k) {

$sql.="OR c.c_name LIKE '%$k%' OR sm.sm_brand_name LIKE '%$k%' OR ca.cat_name LIKE '%$k%' OR c.c_description LIKE '%$k%'";
}

$result = mysqli_query($con, $sql);
$count=mysqli_num_rows($result);
if($count!=0) {
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo $row['c_name'];

echo $row ['c_description'];
echo $row['sm_brand_name'];
echo "<br>";
}
}
else
{
echo "no result";
}

最佳答案

submit(function(){ 中的变量 event 未定义。

将其作为函数的参数提供:

$("document").ready(function(){
$(".but").submit(function(event){ // here
event.preventDefault();
var $form = $( this ),
var myData=$form.find( 'input[name="query"]' ).val(),
$.ajax({
url: 'result.php',
data: myData,
type: 'post',
dataType: "html",
success: function(result){
$('.coupons').text(result)
}
});
});
});

关于javascript - 在同一页面的 div 中显示 ajax 调用结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44090943/

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