php - Get variable $_GET with ajax -
i'm trying build ajax live search.
index.php
<div class="form-group"> <div class="input-group"> <span class="input-group-addon">search</span> <input type="text" name="search_text" id="search_text" class="form-control" /> </div> </div> <div id="result"></div> <script> $(document).ready(function() { load_data(); function load_data(query) { $.ajax({ url:"new/new_fetch.php", method:"post", data:{query:query}, success:function(data) { $('#result').html(data); } }); } $('#search_text').keyup(function() { var search = $(this).val(); if(search != '') { load_data(search); } else { load_data(); } }); }); </script>
new/new_fetch.php
$connect = mysqli_connect("localhost", "root", "root", "dbname"); if(isset($_post["query"])) { $query = "select * subcategories category_parent = '".$_get['category']."' order subcategory"; }
but have undefined index: category in new/new_fetch.php
error.
my code works without operator.
how can $_get['category']
using ajax post method? thx
the error because never passed category variable in ajax. need below:
$.ajax({ url:"new/new_fetch.php", method:"get", data:{ query:query, category:"some_value" }, success:function(data) { $('#result').html(data); } });
update
if want accept both $_get , $_post recommend use $_request covers both , post.
Comments
Post a Comment