THAT Agency Design Studio Blog

Recently we had a client that wanted the following:

1. They wanted to have an auto suggest based on input of the user for cities.

2. They also wanted “categories” in the form of radio buttons that would also be used to return some results.

So I used jQuery to accomplish this. Attached you will find the files. Some things to note is that you obviously have to place the jQuery file in there.

The Gist: Basically a user types the beginning of a city name. The code will execute the auto-suggest and return results based on “keyup” once the “city” from the list is clicked, it will enable the radio buttons. these radio buttons will control the results you see. change the radio button and see different results based on what the value of the radio button is as well as the city in the search box. Sounds interesting? here’s how I did it.




Javascript


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// BEGIN DOM
$(function()
{
	$("input:radio[name=utilCat]").attr("disabled","disabled");
	$("#cityName").keyup(function(){
		var searchTerm = $("#cityName").attr("value"); 
		sendValue(searchTerm);
	});
 
 
 
	if($.browser.msie){
		$("input:radio[name=utilCat]").click(function(){
			getInformation();
		});
	}
	else {
		$("input:radio[name=utilCat]").change(function(){
			getInformation();
		});
	}
 
});
 
function sendValue(searchTerm){
	cleanUp();
	var searchTerm;
	$.post("/citysearch.php", { cityIs: searchTerm }, 
    function(data){
		$("#suggest").html(data);
		$("#suggest").show();
		$("#suggestLink a").click(clicked);
	});
	cleanUp();
	return false;	
}
 
function clicked(){
 
	var okMan = $(this).attr("rel");
	$("#cityName").attr("value",okMan);
	$("#suggest").hide();
	$(".utilRadio").removeAttr('disabled');	
	var searchTerm = $("#cityName").attr("value");
	getInformation();
	cleanUp();
	return false;
}
 
function getInformation(){
	var searchTerm = $("#cityName").attr("value");
	var okMan = $("input:radio[name=utilCat]:checked").val();
	$.post("/getBanners.php", { cityIs: searchTerm, categoryID: okMan }, 
    function(data){
		$("#utilContentBox").html(data);
		$("#utilContentBox .simple").hide();
	});
	cleanUp();
	return true;
}
function cleanUp(){
	$("input:radio[name=utilCat]").unbind('click', clicked);
	$("input:radio[name=utilCat]").unbind('click', getInformation);
}




HTML Form : This will post when changes to the form are made (so there is no submit button)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<form action="/search-utilities-results.php" name="utilForm" id="utilForm" enctype="multipart/form-data" method="post">
	<label for="stateName">State</label><br/>
	<input type="text" name="stateName" id="stateName" value="florida" disabled="disabled" /><br/><br/>
	<label for="cityName">City</label><br/>
	<input type="text" name="cityName" id="cityName" value="" />
	<div id="suggest"></div>
 
    <ul class="mUtilList">
    	<li><input type="radio" name="utilCat" id="utilCat" value="1"  class="utilRadio" checked="checked" /> Change of Address</li>
        <li><input type="radio" name="utilCat" id="utilCat" value="4"  class="utilRadio"  /> Phones</li>
        <li><input type="radio" name="utilCat" id="utilCat" value="2"  class="utilRadio"  /> Electric</li>
        <li><input type="radio" name="utilCat" id="utilCat" value="7"  class="utilRadio"  /> Cable / Satellite</li>
        <li><input type="radio" name="utilCat" id="utilCat" value="3"  class="utilRadio"  /> Water / Sewer / Trash</li>
        <li><input type="radio" name="utilCat" id="utilCat" value="6"  class="utilRadio"  /> High Speed Internet</li>
    </ul>
</form>




CitySearch.php page


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
$search = strtolower($_POST['cityIs']);
$sql = "SELECT utilCityName from util_city WHERE utilCityName LIKE '$search%' ORDER BY utilCityName LIMIT 10";
$result = mysql_query($sql) or die ('cannot perform query because: ' . mysql_error());
$num = mysql_num_rows($result);
if($num > 0){
	echo "<div id=\"suggestLink\">";	
	while ($word = mysql_fetch_object($result)){
		print("<a href=\"#\" rel=\"$word->utilCityName\">$word->utilCityName</a>");
	}
	echo "</div>";
}
else{
	echo "<span style=\"margin:0;padding:2px 0;\">City was not found</span>";
}
?>




GetBanners.php : used to get the actual information needed


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
$cityName = strtolower($_POST['cityIs']);
$catID = strtolower($_POST['categoryID']);
$sql = "SELECT utilAdID, utilAdDate, utilAdTypeID, utilAd, utilAdCity, utilAdName, utilCategoryID from util_ads WHERE utilCategoryID = '$catID' AND utilAdCity = '$cityName' ORDER BY utilAdDate DESC";
$result = mysql_query($sql) or die ('cannot perform query because: ' . mysql_error());
$num = mysql_num_rows($result);
if($num > 0){
	while ($banner = mysql_fetch_object($result))
	{
		print("<div style=\"text-align:left;\">");
		echo nl2br($banner->utilAd);
		print("</div>
			<div class=\"clear\"></div>
			<div class=\"clear10\"></div>
			<div class=\"clear\"></div>
		");						
	}
}
else{
	echo "<span style=\"margin:0;padding:2px 0;\">There are currently no results for your search.</span>";
}
?>

Go ahead and download it now and play around with it. I would love to hear some comments or maybe a simpler way to do this. We as developers always love to learn. get the “auto suggest with results based on text input and radio button now

Share this article

Now lets say that you a web page that has a big Block of “something” and inside this “something” you would like to have some information change at a set time interval. Well with Jquery, some PHP and a sprinkle of Ajax, you can get this done quick fast and in a hurry.

The first thing you will need, is obviously the latest jQuery Javascript library file.  and you will also need this light JQuery Timers Plugin.  Now lets set up:

I am going to assume that you know a little about JQuery and say that you already know how to call from inside the “Head” tag. The following is basically the “meat and potatoes” if you will of what is going on.

Let’s Break it down.

1.  First we say “Hey you with the class name of refreshMe, I am going to do something to you every 10 seconds.  (SideNote * 1000 = 1 second; So 5,000 = 5 seconds).

2. Now we use JQuery’s built in Ajax functions to call the URL of the page we want to bring in and have it refresh the element with the class name of refreshMe with the information from this page that called. Follow? Good.

Now since for some deranged reason, I can’t get the code highlighter to work correctly Click here to see the page in action. You will be able to view source from there.

Or download it now.

Share this article