Reply
Ajax drop down menu not passing variables
Old 09-06-2006, 10:26 PM Ajax drop down menu not passing variables
Junior Talker

Posts: 4
The goal is to display different data depending on what the user inputs. The text box variables are being passed to the .php file but the drop down menu variables are not being passed to the .php file?

<html>
<body>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!

try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var age = document.getElementById('age').value;
var wpm = document.getElementById('wpm').value;
var sex = document.getElementById('sex').value;

var queryString = "?age=" + age + "&wpm=" + wpm + "&sex=" + sex;
ajaxRequest.open("GET", "ajax.php" + queryString, true);
ajaxRequest.send(null);
}
//-->
</script>
<form name='myForm'>
<p> Age:
<input type='text' id='age' />
<br />
WPM:
<input type='text' id='wpm' />
<br />
Sex:
<select name="sex" id='sex' onChange="onreadystatechange(sex);">
<option>m</option>
<option selected>f</option>
</select>
</p>
<p>
<input type='button' onclick='ajaxFunction()' value='submit' />
</p>
</form>
<div id='ajaxDiv'>Your result will display here</div>
</body>
</html>
quickz is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 09-07-2006, 04:01 AM Re: Ajax drop down menu not passing variables
Super Spam Talker

Posts: 893
Your code there looks right on the money ..

chuck a copy of ajax.php here up so I can check that for you
Sir P is offline
Reply With Quote
View Public Profile
 
Old 09-07-2006, 06:16 AM Re: Ajax drop down menu not passing variables
Ultra Talker

Posts: 253
Location: Auckland, New Zealand
You need more information for your options, there's no value specified and I'm not sure whether it's meant to default to the value of the label, or content but it should be written as such (however I extended the label to reflect what it is, the Value however is what will be sent), also, attributes are case sensitive and should be lowercase:

HTML Code:
<select id="sex" name="sex" onchange="onreadystatechange(sex)">
  <option label="Male" value="M">Male</option>
  <option label="Female" value="F" selected="selected">Female</option>
</select>
Other noticeable problems is that you're not encoding the values you're using in the URI:

Code:
var queryString = "?age=" + age + "&wpm=" + wpm + "&sex=" + sex;
SHOULD BE:
var queryString = "?age=" + encodeURIComponent(age) + "&wpm=" + encodeURIComponent(wpm) + "&sex=" + encodeURIComponent(sex);
And more problems, the form is not well formed, does not contain the minimum requirements of a form, and relies on Javascript for it to work, why not have an actual submit button, a form action, etc and if you don't want submission to take place, return false on the onsubmit attribute for the form, though it's best to use a function that will either allow the submission to take place or not.

Hope this helps.

Cheers,

MC
__________________
#------------------------------signature---------------------------------------------------------------------------------#
Quote:
I am well recognised for what I don't do than what I do. Chores are just one of those things.

Last edited by mastercomputers : 09-07-2006 at 06:20 AM.
mastercomputers is offline
Reply With Quote
View Public Profile Visit mastercomputers's homepage!
 
Reply     « Reply to Ajax drop down menu not passing variables
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.14038 seconds with 12 queries