|
PHP:Ajax |
<html> <head>
<title>Using Ajax and XML dropdown list</title>
<script language = "javascript"> var options; var xml_req_obj = false; if (window.XMLHttpRequest) { xml_req_obj = new XMLHttpRequest(); xml_req_obj.overrideMimeType("text/xml"); } else if (window.ActiveXObject) { xml_req_obj = new ActiveXObject("Microsoft.XMLHTTP"); } function getoptions1() { if(xml_req_obj) { xml_req_obj.open("GET", "options1.php", true); xml_req_obj.onreadystatechange = function() { if (xml_req_obj.readyState == 4 &&
xml_req_obj.status == 200) { var xmlDocument = xml_req_obj.responseXML; options = xmlDocument.getElementsByTagName("option"); listoptions(options); } } xml_req_obj.send(null); } } function getoptions2() { if(xml_req_obj) { xml_req_obj.open("GET", "options2.php", true);
xml_req_obj.onreadystatechange = function() { if (xml_req_obj.readyState == 4 && xml_req_obj.status == 200) { var xmlDocument = xml_req_obj.responseXML; options = xmlDocument.getElementsByTagName("option"); listoptions(); } } xml_req_obj.send(null); } } function listoptions () { var loopIndex; var selectControl = document.getElementById('optionList');
for (loopIndex = 0; loopIndex < options.length; loopIndex++ ) { selectControl.options[loopIndex] = new Option(options[loopIndex].firstChild.data); } }
function setoption() { document.getElementById('div_1').style.color = options[document.getElementById ('optionList').selectedIndex].firstChild.data; }
</script> </head>
<body text="#000080" bgcolor="#C0C0C0">
<h1>retreive data in a Drop down list:from server </h1>
<form> <select size="1" id="optionList" onchange="setoption()"> <option>Select a scheme</option> </select> <input type = "button" value = "Use color scheme 1" onclick = "getoptions1()"> <input type = "button" value = "Use color scheme 2" onclick = "getoptions2()"> </form>
<div id="div_1" width =100 height=100>Color this text.</div>
</body>
</html>
|
code option_1.php<? header("Content-type: text/xml"); $options = array('red', 'green', 'blue'); echo '<?xml version="1.0"?>'; echo '<options>'; foreach ($options as $value) { echo '<option>'; echo $value; echo '</option>'; } echo '</options>'; ?>
|
code option_2.php<? header("Content-type: text/xml"); $options = array('black', 'white', 'orange'); echo '<?xml version="1.0"?>'; echo '<options>'; foreach ($options as $value) { echo '<option>'; echo $value; echo '</option>'; } echo '</options>'; ?>
|

|

|