AJAX jquery 没有响应并显示 div



我有一个简单的表单,在其中我动态检索数据,然后将其发送到另一个页面。使用该数据,我希望在我的页面上显示一些div。当我简单地不使用 AJAX 检查它时,它会返回div。但是现在我已经应用了一些 AJAX,但它不起作用。请提出任何建议。

阿贾克斯

$("document").ready(function() {
    $("#search_keyword").on("submit", function (e) {
        e.preventDefault();
        $.post("keyword_search.php?query="+encodeURIComponent($("#keyword").val())+"category="+encodeURIComponent($("#category").val())+"store="+encodeURIComponent($("#store").val()), function (data) {
            var res = JSON.parse(data);
            if (res.divs) {
                $('#search_result').html("");
                for (var i = 0; i < res.divs.length; i++) {
                    $('#search_result').append(res.divs[i]);
                }
            } else {
                $('#search_result').html("No matched coupons found !");
            }
        });
    });
});

形式

<form class="form-horizontal select-search" id="search_keyword" method="post">
    <label class="control-label ">Keyword</label>
    <input class="form-control" id="keyword" name="keyword" type="text">
    <!-- Select Category -->
        <label class="control-label " for="category">Select category</label>
        <select class="category" id="category" name="category">
            <?php 
                $sm=mysqli_query($con,"select * from categories ");
                while ($row1 = mysqli_fetch_array($sm,MYSQLI_ASSOC)){
                    $cat_id = $row1['cat_id'];
                    $name = $row1['cat_name'];
                    echo '<option value="' . $cat_id . '">' . $name . '</option>';
                }  
            ?>
        </select>
    <label class="control-label " for="store">Select a store</label>
    <select class="storesname" id="store" name="store">
      <option selected="selected">Select Stores</option>
   </select>
   <button id="search_btn" name="search_btn" class="btn btn-danger">Search coupons</button>
</form>
<div id="search_result"> </div>
您需要

button更改为submit类型,以便它可以实际提交。

所以改变:-

<button id="search_btn" name="search_btn" class="btn btn-danger">Search coupons</button>

自:-

<input type="submit" id="search_btn" name="search_btn" class="btn btn-danger" value="Search coupons"/>

注意:- 确保在脚本代码之前添加了jQuery库,以便它可以工作。

更改您的代码,如下所示:-

$("document").ready(function() {
    $("#search_keyword").on("submit", function (e) {
        e.preventDefault();
        var data = {'query':encodeURIComponent($("#keyword").val()),'category':encodeURIComponent($("#category").val()),'store':encodeURIComponent($("#store").val())};
        $.post("keyword_search.php",data, function (data) {
            var res = JSON.parse(data);
            if (res.divs) {
                $('#search_result').html("");
                for (var i = 0; i < res.divs.length; i++) {
                    $('#search_result').append(res.divs[i]);
                }
            } else {
                $('#search_result').html("No matched coupons found !");
            }
        });
    });
});

在你的keyword_search.php检查如下:-

<?php
echo "<pre/>";print_r($_POST); //check that how post data are coming
// rest do code accordingly
?>

同时从当前<form>中删除method="post"

你只是为了改变jQuery中的一些东西。

我刚刚将"提交"更改为"单击","#search_keyword"更改为"#search_btn">

$("document").ready(function() {
  $("#search_btn").on("click", function (e) {
  e.preventDefault();
  $.post("keyword_search.php?query=" + encodeURIComponent($("#keyword").val())+encodeURIComponent($("#category").val())+encodeURIComponent($("#store").val()), function (data) {
   var res = JSON.parse(data);
   if (res.divs) {
   $('#search_result').html("");
    for (var i = 0; i < res.divs.length; i++) {
     $('#search_result').append(res.divs[i]);
      }
     } else {
      $('#search_result').html("No matched coupons found !");
    }
            });
        });
    });

它可能会对你有所帮助

最新更新