?
Solved

How to do an AJAX Dropdown

Posted on 2011-09-30
2
Medium Priority
?
192 Views
Last Modified: 2012-06-21
I need to have database-driven dropdown menu, like this one:
echo "<select>";
for($i=0; $i<$num;$i++){
   echo "<option value=\"".trim($row[$i])."</option>\n";
}
echo "</select>";

Open in new window

When an option is selected, I need to run a query to fill a DIV with content from a database.  Can anyone help me with this?


0
Comment
Question by:EMB01
2 Comments
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 36893943
Put this in the head section :
<script language="javascript" type="text/javascript">
	function runAQueryToFillADIVWithContentFromADatabase(PID) {
		if(PID.length==0) {
			document.getElementById("DIV_ID").innerHTML="";
			return true;
		}
		var xmlhttp;
		if(window.XMLHttpRequest) xmlhttp=new XMLHttpRequest();else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		if(xmlhttp==null) {
			alert("your browser don't support Ajax");
		}
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState==4 && xmlhttp.status==200) {
				document.getElementById("DIV_ID").innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.open("GET","getDatabaseDrivenDropdownMenu.php?pid=" + PID,true);
		xmlhttp.send();
	}
</script>

Open in new window


your div (to fill with the dynamic dropdown):
<div id="DIV_ID"></div>

your first dropdown need to have :

<select onchange="runAQueryToFillADIVWithContentFromADatabase(this.value)">

the value of the option (this.value) can be used by the server side (your php script to select/filter some of the data and build the option)

Need more infos?
Take your time to read this tuto for example : http://www.w3schools.com/ajax/default.asp
0
 
LVL 14

Author Closing Comment

by:EMB01
ID: 36903042
Perfect, very simple implementation.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question