NewWebDesigner
asked on
Can't get AJAX to work, (very simple)
Hello. I have two drop down boxes, upon the selection of an option in the second box, I would like this values sent to a PHP server using AJAX and printed in the appropriate div. Why is my code not working? Thank you.
Homepage
Homepage
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function showCars(car1, car2)
{
var xmlhttp;
if (car1.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","gethint3.php?q="+car1 + "r&" + car2,true);
xmlhttp.send();
}
</script>
</head>
<body>
<select name='one' id="one">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<select name="two" onchange="showCars(this, document.getElementById('one');">
<option value="volvo" name="two">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<div id="txtHint"></div>
</body>
</html>
<?php
$q=$_GET["q"];
echo $q;
$r=$_GET["r"];
echo $r;
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Dave Baldwin, What is "formx?" I have never seen that before.
Two changes
xmlhttp.open("GET","gethin t3.php?q=" +car1 + "&r=" + car2,true);
and
<select name="two" onchange="showCars(this.op tions[this .selectedI ndex].valu e, document.getElementById('o ne').optio ns[documen t.getEleme ntById('on e').select edIndex].v alue);">
your existing code is not working because there is a typo mistake in your html select code. You have not closed the showCars call in select element
xmlhttp.open("GET","gethin
and
<select name="two" onchange="showCars(this.op
your existing code is not working because there is a typo mistake in your html select code. You have not closed the showCars call in select element
"formx" is the name of the form. I put the <select> statements in a <form> because it was an easy way (that I've done before) to get the value of the currently selected option. There is probably another way but this is one I've done.
Here is an alternative way to get the currently selected value: http://www.w3schools.com/jsref/prop_select_selectedindex.asp While the browser will send the selected value when you submit a form, it is a pain to get it in javascript on the same page.
Also in your original code were error:
<select name="two" onchange="showCars(this, document.getElementById('one');">
which should be replaced with<select name="two" onchange="showCars(this, document.getElementById('one'));">
Open in new window