2017-05-23 10 views
2

私はデータを動的に取得して別のページに送信するシンプルなフォームのコードを用意しています。そのデータを使用して、私のページにいくつかのdivが表示されます。 AJAXを使わずにチェックするとdivを返す。しかし、今ではAJAXをいくつか適用していますが、動作しません。どんな提案もお願いします。AJAX jqueryが応答せず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 !"); 
      } 
     }); 
    }); 
}); 

フォームあなたはそれが実際に提出することができるようにbuttonからsubmitタイプに変更する必要が

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

変更ボタンを助けるかもしれない「#search_btn」に「クリック」と「#search_keyword」に「送信」

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

を変更しました –

+0

'

+0

は依然として応答しません。私はそこにajax投稿に問題があると思う投稿 – tabia

答えて

3

ので変更: -

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

+0

@Alvieはまだ私を助けていない – tabia

+1

@ Alvieは完了です!ありがとうございました – tabia

+0

@tabiaあなたを助けてうれしい:) :) –

0

からmethod="post"を削除しますjQurtyでいくつか変更する

私はちょうどそれはあなたにだけ、それはボタンをwork.Becauseます提出する

関連する問題