heals1ic
05-18-2006, 01:51 AM
I have a script that involves replacing dropdown options via ajax and javascript:
<script type="text/javascript">
var url = "pref.php?catid="; // The server-side script
function handleHttpResponse() {
if (http.readyState == 4) {
if(http.status==200) {
var results=http.responseText;
var resultarray = new Array();
resultarray = results.split("|", 3);
for (var i=0; i<3; i++) {
document.getElementById('pref_'+(i+1)).innerHTML = resultarray[i];
}
}
}
}
function requestPrefs() {
var cat = document.getElementById('catselect');
var id = cat.options[cat.selectedIndex].value;
http.open("GET", url + escape(id) + "&userid=<?=$_SESSION['user_id']?>", true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}
function getHTTPObject() {
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
if (!xmlhttp){
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
}
return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object
</script>
<form action="pref.php" method="post">
<select id="catselect" size="7" name="catid" onChange="requestPrefs();">
<option value="1">cat1</option>
<option value="2">cat2</option>
</select>
<select id="pref_1" name="pref_1">
<option value="">-- Select --</option>
</select>
<select id="pref_2" name="pref_2">
<option value="">-- Select --</option>
</select>
<select id="pref_3" name="pref_3">
<option value="">-- Select --</option>
</select>
The data returned in the results variable is a string of option elements with the selected option in this string. The string is separated by a delimeter to allocate the 3 option lists into arrays for the 3 select dropdowns.
This code works fine for Firefox but will not populate the select elements in IE.
<script type="text/javascript">
var url = "pref.php?catid="; // The server-side script
function handleHttpResponse() {
if (http.readyState == 4) {
if(http.status==200) {
var results=http.responseText;
var resultarray = new Array();
resultarray = results.split("|", 3);
for (var i=0; i<3; i++) {
document.getElementById('pref_'+(i+1)).innerHTML = resultarray[i];
}
}
}
}
function requestPrefs() {
var cat = document.getElementById('catselect');
var id = cat.options[cat.selectedIndex].value;
http.open("GET", url + escape(id) + "&userid=<?=$_SESSION['user_id']?>", true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}
function getHTTPObject() {
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
if (!xmlhttp){
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
}
return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object
</script>
<form action="pref.php" method="post">
<select id="catselect" size="7" name="catid" onChange="requestPrefs();">
<option value="1">cat1</option>
<option value="2">cat2</option>
</select>
<select id="pref_1" name="pref_1">
<option value="">-- Select --</option>
</select>
<select id="pref_2" name="pref_2">
<option value="">-- Select --</option>
</select>
<select id="pref_3" name="pref_3">
<option value="">-- Select --</option>
</select>
The data returned in the results variable is a string of option elements with the selected option in this string. The string is separated by a delimeter to allocate the 3 option lists into arrays for the 3 select dropdowns.
This code works fine for Firefox but will not populate the select elements in IE.