×

THATBlog

Auto suggest with results based on text input and radio button

Posted at Aug 19, 2009 12:48:25 PM by Ronald Busky | Share

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

// 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)

<form action="/search-utilities-results.php" name="utilForm" enctype="multipart/form-data" method="post">
	<label for="stateName">State</label>


	<label for="cityName">City</label>
<div></div>
<ul class="mUtilList">
<li> Change of Address</li>
<li> Phones</li>
<li> Electric</li>
<li> Cable / Satellite</li>
<li> Water / Sewer / Trash</li>
<li> High Speed Internet</li>
</ul>
</form>

CitySearch.php page

 0){
	echo "
<div>";
	while ($word = mysql_fetch_object($result)){
		print("<a href="#">utilCityName"&gt;$word-&gt;utilCityName</a>");
	}
	echo "</div>

";
}
else{
	echo "<span style="margin:0;padding:2px 0">City was not found</span>";
}
?&gt;

GetBanners.php : used to get the actual information needed

 0){
	while ($banner = mysql_fetch_object($result))
	{
		print("
<div style="text-align:left">");
		echo nl2br($banner-&gt;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>";
}
?&gt;

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"

Tags: Web Development

RECENT POSTS
Best Practices for Digital Marketing in 2022: FREE GUIDE