Solved

How to do an AJAX Dropdown

Posted on 2011-09-30
2
183 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 82

Accepted Solution

by:
leakim971 earned 500 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now