Solved

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

Posted on 2012-04-05
7
361 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 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
clicking to find my folders on wamp 8 47
How can I split a variable 19 45
Google Recaptcha 13 55
Posting and receiving data with JSON in PHP 12 38
This article discusses four methods for overlaying images in a container on a web page
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

751 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