简体   繁体   中英

AJAX jquery not responding and showing divs

I have code a simple form in which I retrieve data dynamically and then sending it to another page. Using that data i want some divs to be displayed on my page. It is returning divs when I check it simply without using AJAX. But now I have applied some AJAX and it is not working. Any suggestions please.

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

<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>

You need to change from button to submit type so that it can actually submit.

So change:-

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

To:-

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

Note:- Make sure that jQuery library added before your script code so that it will work.

Change your code like below:-

$("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 !");
            }
        });
    });
});

And in your keyword_search.php check like this:-

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

Also remove method="post" from your current <form>

You just to change some thing in jQuery.

I have just changed "submit" to "click" and "#search_keyword" to "#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 !");
    }
            });
        });
    });

It might help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM