Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-25-2013, 09:07 AM   PM User | #1
treigh
New Coder

 
Join Date: Jan 2008
Posts: 37
Thanks: 5
Thanked 0 Times in 0 Posts
treigh is an unknown quantity at this point
How to get autocomplete data from MYSQL based on field searched

Hi there,

I'm using Twitter Bootstrap framework for autocomplete data to suggest a city names to users as they're filling up a contact form. .after
watching a a tutorial on how to pull remote MYSQL data using Bootstrap typeahead and JSON, I'm now able to make city names available to the user. Now I'd like to take this a step further. My solution only works with one field. I'd also like to suggest postal codes to the user the same way I do it with city names.

Here are my codes
Frontend
Code:
    <script>
	$(function(){
		$('[rel=cities]').typeahead({	
			source: function (query, process) {
				$.ajax({
					url: 'data.php',
					type: 'POST',
					data: 'query=' + query,
					dataType: 'JSON',
					async: 'true',
					success: function(data) {
						//console.log(data);
						process(data);
					}
				});
		
			}
		
		});
	});
    </script>
Backend
Code:
<?php

if (isset($_POST['query'])) {

	// Database connect
	mysql_connect("localhost", "root", "pass");
	mysql_select_db ("data");
	
	// Retrieve the query
	$query = $_POST['query'];
	
	//search the database for unique city names based on query
	$sql = mysql_query ("SELECT DISTINCT asciiname FROM cities WHERE asciiname LIKE '%{$query}%'");
	$array = array();
	
	while ($row = mysql_fetch_assoc($sql)) {
	
	$array[] = $row['asciiname'];
	}
	//Return the JSON Array
	
	echo json_encode ($array);

}

?>
I'm very new to PHP and I apologize already if the question barely makes sense.

Thanks for any help.
treigh is offline   Reply With Quote
Old 01-25-2013, 08:36 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Though you *could* use the same "data.php" page to do this, you'd likely find it easier to create another page specifically for the zip codes. Maybe "datazip.php".

Change the query and the fetch line:
Code:
	$sql = mysql_query ("SELECT DISTINCT zip FROM cities WHERE zip LIKE '{$query}%'");
        ...
 	$array[] = $row['zip'];
Notice that I removed the first % after LIKE. If you don't do that, and the user types "9", then you will find "90105", yes, but you will also find "10019". That is, then "9" can be anywhere in the zip code. By removing that first %, at least the zip code must *START* with what the user typed in. (It's possibly a change you might want to make with your cities query...up to you.)

And then of course create another jQuery function, this one with
Code:
		$('[rel=zip]').typeahead({
I don't use jQuery, so I don't know if you need to make other changes there (ask in the jQuery forum), but I don't think so.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:27 PM.


Advertisement
Log in to turn off these ads.