php - Ajax Not Executed -
i beginner ajax world , trying call contents php page using $.ajax() function , code couldn't executed. html page used:
<!doctype html> <html> <head> <title>ajax</title> </head> <body> <div > <input type="text" name="search" id="search"> <br> <br> <h2 id="result"></h2> </div> <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-bbhdlvqf/xty9gja0dq3hiwqf8lacrtxxzkrutelt44=" crossorigin="anonymous"></script> <script src="js/script.js"></script> </body> </html>
the jquery code used in script.js:
$(document).ready(function() { $('#search').keyup(function () { var search = $('#search').val(); $.ajax({ url:'search.php', //the page request go data:{search: search}, type: 'post', success:function(data) { if(!data.error){ $('#result').html(data);//the h2 want echo uing ajax } } }); }); });
the search.php page contain:
$search = $_post['search']; echo $search;
the code not executed. should do.
i see issue in response php code , in ajax side success code.
you not sending in response json format data.error meaningless. in success callback code should this.
success:function(data) { $('#result').html(data);//the h2 want echo uing ajax }
Comments
Post a Comment