?
Solved

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

Posted on 2012-04-05
7
Medium Priority
?
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 84

Accepted Solution

by:
Dave Baldwin earned 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 84

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 84

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article discusses four methods for overlaying images in a container on a web page
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

718 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