...

View Full Version : Radio button problem in AJAX



Midgard
11-20-2006, 03:55 PM
Hi,
I have this ajax script
ajax.js


var http_request = false;
function update(formid,divid){
document.getElementById(divid).innerHTML="Please wait ..."
var form = document.getElementById(formid);
var http_request = false;
http_request = false;
if (window.XMLHttpRequest) {
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml; charset=iso-8859-9');
}
} else if (window.ActiveXObject) {
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XML HTTP instance.');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('POST', "color.php", true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Connection", "close");
http_request.send(createQuery(form));
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
document.getElementById(divid).innerHTML=http_request.responseText;
}else { alert('An error occurred! Please try again.');}
}
}
}
function createQuery(form)
{
var elements = form.elements;
var pairs = new Array();
for (var i = 0; i < elements.length; i++) {
if ((name = elements[i].name) && (value = elements[i].value))
pairs.push(name + "=" + encodeURIComponent(value));
}
//pairs.push("param1=1");
return pairs.join("&");
}

And, i'm using this form.
form.php


<form style="margin: 0px;" name="form1" id="form1" method="post" action="">
<p><input type="radio" name="color_id" value="1"></p>
<p><input type="radio" name="color_id" value="3"></p>
<p><input type="radio" name="color_id" value="5"></p>
<p><input type="radio" name="color_id" value="7"></p>
<input type="button" value=" Ok " onClick="javascript:update('form1','SeCiLeNReNK');">
</form>

And this code shown selected color.
color.php


<?php
echo '<pre>';
print_r($_POST);
echo '</pre>';
?>

But, result always '7' ... I'm checked 1, 3 or 5 .. But result always 7 ...
Please help me.
Thanks.

mlseim
11-20-2006, 05:45 PM
Didn't test it out myself, but try this:

<input type="radio" name="color_id" id="color_id" value="1">

I mention doing that because you have: getElementById

It's looking for an ID, not a NAME.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum