Jump to content

Recommended Posts

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;

});

Link to comment
https://forums.phpfreaks.com/topic/217606-ajaxjquery/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/217606-ajaxjquery/#findComment-1129710
Share on other sites

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;

});

Link to comment
https://forums.phpfreaks.com/topic/217606-ajaxjquery/#findComment-1130034
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.