Solved

Can't get AJAX to work, (very simple)

Posted on 2012-04-05
7
352 Views
Last Modified: 2012-06-28
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

0
Comment
Question by:NewWebDesigner
7 Comments
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 37815144
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

0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 37815147
Or 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 type="text/javascript">
function showCars()
{
ndxcar1 = document.forms.formx.one.selectedIndex;
car1 = document.forms.formx.one.options[ndxcar1].value;
ndxcar2 = document.forms.formx.two.selectedIndex;
car2 = document.forms.formx.two.options[ndxcar2].value;
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>
<form action="" method="" name="formx">

   <select name='one' id="one">
	  <option>Select One</option>
    <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" onchange="showCars();">
	  <option>Select Two</option>
    <option value="volvo" name="two">Volvo</option>
    <option value="saab">Saab</option>
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
   </select>
  
</form>
 <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

0
 

Author Comment

by:NewWebDesigner
ID: 37815202
Dave Baldwin,  What is "formx?"  I have never seen that before.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:Dushyant Sharma
ID: 37815230
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
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37815270
"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.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37815296
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.
0
 
LVL 4

Expert Comment

by:4e4en
ID: 37815582
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

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question