...

Ajax and Radio Buttons Problem!

shanparker
07-13-2007, 12:13 AM
I've looked this up online and since I'm not great with neither AJAX or JS I'm a little bit at a loss. I read that I needed to use ID tags, but that still didn't get me far. Can anyone take a look at this and help me figure out why the radio buttons don't send to the php script?

Thanks in advance!!

<?
$query = "SELECT * FROM spicegirls_tour ORDER BY 'id' ASC";
$result=mysql_query($query);
while($row = mysql_fetch_array($result)){ // loop through the result of the page.
?>
<tr>
<td><input type="radio" name="local" id="local[<? echo $row[id]; ?>]" value="<? echo $row[id]; ?>"></td>
<td><? echo $row[local]; ?></td>
</tr>
<?
}
?>

<script language="Javascript">

// You don't need to change anything in this function:

function xmlhttpPost(strURL) {
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
updatepage(self.xmlHttpReq.responseText);
}
}
self.xmlHttpReq.send(getquerystring());
}

// The following function obtains two variables from your form (email and message)
// and builds a string that gets sent to your PHP script. Change this function to
// obtain whatever fields you want from your form.

function getquerystring() {
var form = document.forms['form1'];
var local = form.local.value;
var username = form.username.value;
qstr = 'local=' + escape(local) + '&username=' + escape(username);
return qstr;
}

function updatepage(str){
document.getElementById("result").innerHTML = str;
}
</script>

mcjwb
07-13-2007, 12:51 PM
You have multiple radio buttons all named "local", your user clicks on one radio button and it becomes checked.
The line "var local = form.local.value;" simply doesn't make sense as form.local will reference a collection of inputs (which won't have a value property).

You need to loop round the collection of radio buttons, find out which one's been checked and then use the value from the checked radio button.

Replace "var local = form.local.value;" with the following;

var local;
for (var i = 0; i < form.local.length; i++) {
if(form.local[i].checked){
local = form.local[i].value;
}
}

glenngv
07-13-2007, 07:13 PM
It's good to have the break statement once the checked radio is found in order to keep the loop from doing unnecessary iteration.

var local;
for (var i = 0; i < form.local.length; i++) {
if(form.local[i].checked){
local = form.local[i].value;
break;
}
}

shanparker
07-13-2007, 11:03 PM
Thanks so much guys! This worked perfectly!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum