c_pattle Posted November 2, 2010 Share Posted November 2, 2010 I am trying to use jquery for my AJAX request but am having a bit of trouble. My AJAX works but I am not sure how to get the response from the PHP page. My PHP script echo's some text but I am not sure how to append this text. I am trying to use data but I'm not sure if that's right. My script is below. $("#search").change(function() { var searchterm = $("input#search").val(); var searchString = 'search_term='+ searchterm; $.ajax({ type: "POST", url: "update_location.php", data: searchString, success: function(data) { $('#search_bar_hints').append(data); } }); return false; }); Quote Link to comment https://forums.phpfreaks.com/topic/217606-ajaxjquery/ Share on other sites More sharing options...
Twitch Posted November 3, 2010 Share Posted November 3, 2010 I'm going on the assumption from the words "input" and "search" in your code that you're trying to do an autocomplete feature on a text field? If so, I use jqueryui library http://jqueryui.com/ and use the javascript below. <script type='text/javascript'> $(function() { $('#search').autocomplete({ source: 'update_location.php', minLength: 2 //number of characters to trigger the autocomplete }); }); </script> Then your update_location.php file would have <?php $return_arr = array(); //added to prevent sql injection $term = $_GET['term']; $hostname = "your connection"; $database = "your database name"; $username = "database username"; $password = "your password"; $term = mysql_real_escape_string($term);//prevent sql injection $conn = mysql_connect($hostname, $username, $password) or die ('Error connecting to mysql'); mysql_select_db($database); /* If connection to database, run sql statement. */ if ($conn) { $fetch = mysql_query("SELECT * FROM your_table where your_column like '%" . $term . "%'"); /* Retrieve and store in array the results of the query.*/ while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) { // $row_array['value'] = $row['your_column']"; array_push($return_arr,$row_array); } } /* Free connection resources. */ mysql_close($conn); /* Toss back results as json encoded array. */ echo json_encode($return_arr); ?> I haven't tested this but the general idea should work. Of course you need the jqueryui library and you need to link to it on the page you have the search input on. If you weren't looking for this I apologize. I ask for help on here and I like to return the favor...haha -Twitch Quote Link to comment https://forums.phpfreaks.com/topic/217606-ajaxjquery/#findComment-1129710 Share on other sites More sharing options...
c_pattle Posted November 3, 2010 Author Share Posted November 3, 2010 Thanks, that was helpful and I will use that script. However I just want to know how to get data returned from the server in an AJAX resquest. For example if the PHP script returns "yes", how do I access that? Quote Link to comment https://forums.phpfreaks.com/topic/217606-ajaxjquery/#findComment-1129817 Share on other sites More sharing options...
Twitch Posted November 3, 2010 Share Posted November 3, 2010 Ah sorry, I wasn't thinking clearly trying to deal with a code problem I was having haha. How about: success: function(data){ $("#search_bar_hints").ajaxComplete(function(event, request, settings){ $(this).html(data); } }); } Quote Link to comment https://forums.phpfreaks.com/topic/217606-ajaxjquery/#findComment-1129894 Share on other sites More sharing options...
c_pattle Posted November 3, 2010 Author Share Posted November 3, 2010 Thanks for your help although I am still getting a few problems. It still doesn't seem to be returned any data. However I get no errors returned in the error console so I think maybe there is a problem with my php script. Here is my ajax request. Also I have just started using JQuery so feel free to point out if I am doing anything wrong (e.g could write better code). $("#search_submit").click(function() { var search_term = $("input#search").val(); var dataString = 'search_term='+ search_term; $("#loading_status").ajaxStart(function(){ $("#loading_status").html('<img src="loading.gif"/\> '); setTimeout(function(){ $("#loading_status").html('')},1000); }); $.ajax({ type: "POST", url: "update_location.php", data: dataString, success: function(data) { $('#lightbox_content').hide(); $('#login_content').show(); $("#login_content").ajaxComplete(function(event, request, settings){ $("#login_content").html(data); }); } }); return false; }); Quote Link to comment https://forums.phpfreaks.com/topic/217606-ajaxjquery/#findComment-1130034 Share on other sites More sharing options...
Twitch Posted November 3, 2010 Share Posted November 3, 2010 one thing that may be stalling the javascript is the \ you have in the <img /> tag This: <img src="loading.gif"/\> should: <img src="loading.gif"/> Quote Link to comment https://forums.phpfreaks.com/topic/217606-ajaxjquery/#findComment-1130078 Share on other sites More sharing options...
Twitch Posted November 3, 2010 Share Posted November 3, 2010 it may also help to see your php code. Quote Link to comment https://forums.phpfreaks.com/topic/217606-ajaxjquery/#findComment-1130079 Share on other sites More sharing options...
c_pattle Posted November 3, 2010 Author Share Posted November 3, 2010 I've made such a stoopid mistake. I was using a $_GET in my PHP script instead of POST. Sorry to waste your time. All your help was gratefully received. Quote Link to comment https://forums.phpfreaks.com/topic/217606-ajaxjquery/#findComment-1130117 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.