Solved

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

Posted on 2012-04-05
7
302 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 82

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 14

Expert Comment

by:ddsh79
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 82

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 82

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now