Forum Moderators: open
<select name="order">
<option selected disabled style="color:#cccccc">Categorie</option>
<option onmouseup="javascript:replace2('prod', 'Brood')">Brood</option>
<option onmouseup="javascript:replace2('prod', 'Soepen')">Soepen</option>
<option onmouseup="javascript:replace2('prod', 'Drinken')">Drinken</option>
</select>
The AJAX code:
function replace2(where, what) {
toggleLayer('commentForm');
http.open("GET", "prodDropdowns.php?cat=" + what, true);
http.onreadystatechange=function() {
if(http.readyState == 4) {
document.getElementById(where).innerHTML = http.responseText;
}
}
http.send(null);
toggleLayer('commentForm');
}
Thanks in advance,
Stefan
<select onChange="replace2('prod',this.options[this.selectedIndex].value); return false;">
<option value='xx' selected disabled style="color:#cccccc">Categorie</option>
<option value='soep'>Soep</option>
<option value='broodjes'>Broodjes</option>
</select>
function replace2(Where,What) {
if (What!= 'xx') {
whatever you want to do here
}
}
I use a value of 'xx' for disabled items in the list because I found out that IE6 doesn't understand 'disabled' and check on this value in the replace2 function.
Complete Javascript/AJAX code:
image1 = new Image();
image1.src = "files/loader.gif";
if(window.XMLHttpRequest){
http = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
http=new ActiveXObject("Msxml2.XMLHTTP");
if (! request){
http=new ActiveXObject("Microsoft.XMLHTTP");
}
}
function replace2(where,what) {
if (What!= 'xx') {
alert ('Kies a.u.b. een categorie');
}
else {
toggleLayer('commentForm');
http.open("GET", "catDropdowns.php?kind=" + what, true);
http.onreadystatechange=function() {
if(http.readyState == 4) {
document.getElementById(where).innerHTML = http.responseText;
}
}
http.send(null);
toggleLayer('commentForm');
}
}
function toggleLayer(whichLayer) {
var elem, vis;
if( document.getElementById )
elem = document.getElementById( whichLayer );
else if( document.all )
elem = document.all[whichLayer];
else if( document.layers )
elem = document.layers[whichLayer];
vis = elem.style;
if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
vis.display = (vis.display==''¦¦vis.display=='block')?'none':'block';
}
PHP Code (content to replace the div):
if (!isset($_GET['kind']) ¦¦ $_GET['kind'] == "lunch") {
PRINT <<<HERE
<select onChange="replace2('prod',this.options[this.selectedIndex].value); return false;" name="order">
<option value='xx' selected disabled style="color:#cccccc">Categorie</option>
<option value='Brood'>Brood</option>
<option value='Soepen'>Soepen</option>
<option value='Drinken'>Drinken</option>
</select>
HERE;
}
What exactly isn't working?
If you put only an alert in the replace2 function to display the arguments, are they correct?
If that works, insert the XMLHttpRequest in the function and check whether it calls the catDropdowns.php script on your server, don't use the togglelayer stuff yet.
If that works, call that same script with the same arguments just right from your browser and check whether it returns what you expect.
If that works, add the togglelayer stuff again and check whether that works.
By then you should have the same as you have now, so on the way there something should fail.
Regards,
Arjan
According to Firebug (firefox), the script does call the file, but never receives anything (on the "Response" tab, it keeps saying "Loading...").
function replace2(where,what) {
if (what!= 'xx') {
if(window.XMLHttpRequest){
http = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
http=new ActiveXObject("Msxml2.XMLHTTP");
if (! request){
http=new ActiveXObject("Microsoft.XMLHTTP");
}
}
http.open("GET", "prodDropdowns.php?kind=" + what, true);
http.onreadystatechange=function() {
if(http.readyState == 4) {
document.getElementById(where).innerHTML = http.responseText;
}
}
http.send(null);
}
}