Join Date: November 2008
Location: India ( chennai )
Posts: 14
select option in php ajax
Hi,
Can anyone give me the code to select option in ajax..For Example,
Im having two select box say,in first select box i have the option A,B,C etc..When i select A,from the server side i should do some operation and the output should shown in the second select box.Remember it should be in ajax and the poster,plz explain the code u post.
Thanks with advance, thiyaghu.
benaya
Posted on Jan 2, 2009
Join Date: June 2007
Location: India ( Chennai )
Posts: 90
RE : select option in php ajax
Hi thiyaghu, Here is the code for what you want. I have created two page one as test.html and another one as server.php.
test.html -----------Begin Here----------------------- <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Ajax Code</title> <script type="text/javascript"> function ajaxLoad() { var id = document.getElementById("sel1").value; var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert(e.description); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { //Get values from server page to select box document.getElementById("sel2").innerHTML = xmlHttp.responseText; } }
// Opening url and send to server end xmlHttp.open("GET","server.php?id="+id,true); xmlHttp.send(null); } </script> </head> <body> <select id="sel1" onchange="return ajaxLoad();"> <option value="A">A</option> <option value="B">B</option> <option value="C">C</option> </select> <select id="sel2"> </select> </body> </html> -----------End Here-------------------------
server.php -----------Begin Here----------------------- <?php // Get values from client side $id = $_REQUEST["id"]; switch($id) { case "A": $strOpt = "<option>A1</option>"; break;
case "B": $strOpt = "<option>B1</option>"; break;
case "C": $strOpt = "<option>C1</option>"; break; }
echo $strOpt; ?>
-----------End Here-------------------------
I hope you can understand if you have any doubt let me know.