Link to home
Start Free TrialLog in
Avatar of NewWebDesigner
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

<!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>

Open in new window



<?php
$q=$_GET["q"];
echo $q;
$r=$_GET["r"];
echo $r;
?>

Open in new window

Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

Try this

<!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 src="http://code.jquery.com/jquery-1.7.2.js"></script>
<script type="text/javascript">



  $(document).ready(function(){
    
    
    $('#two').change(function(){
      var url1 = 'gethint3.php?q=' + $('#one').val() + '&r=' + $('#two').val();
      alert(url1);
       $.ajax({
       url: url1,
       dataType: 'text',
       data: {},
       success: function(data){
          $('#txtHint').html(data);
       }
       
     });
    })
      
  
  });

</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" id="two">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
   </select>
  
 <div id="txtHint"></div>

</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NewWebDesigner
NewWebDesigner

ASKER

Dave Baldwin,  What is "formx?"  I have never seen that before.
Two changes

xmlhttp.open("GET","gethint3.php?q="+car1 + "&r=" + car2,true);

and

<select name="two" onchange="showCars(this.options[this.selectedIndex].value, document.getElementById('one').options[document.getElementById('one').selectedIndex].value);">

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');">

Open in new window

which should be replaced with
<select name="two" onchange="showCars(this, document.getElementById('one'));">

Open in new window