views:

193

answers:

2

hello, i'm unable to get a radio value in a form loaded by ajax into a div. here is the javascript code i'm using. (the radio name is 'categorie_add' in get function)

function getCheckedValue(radioObj) {
if(!radioObj)
    return "";
var radioLength = radioObj.length;
if(radioLength == undefined)
    if(radioObj.checked)
        return radioObj.value;
    else
        return "";
for(var i = 0; i < radioLength; i++) {
    if(radioObj[i].checked) {
        return radioObj[i].value;
    }
}
return "";

}

function get(obj) {
    var poststr ="cat_title=" + escape(encodeURI(document.getElementById("cat_title").value )) +
                 "&cat_description=" + escape(encodeURI( document.getElementById("cat_description").value ))+
                 "&cat_id=" + escape(encodeURI( getCheckedValue(document.categorie_add.cat_id) ));
    makePOSTRequest('categorie.php', poststr);
}

and i have this in php file

            echo '<li><input type="radio" name="cat_id" id="cat_id" value="' . $value['cat_id'] . '" /> ' . htmlentities($value['cat_title']) . '<br />';

thank you in advance for your answers.

A: 

I suppose the problem is in the way you catch the radio object in DOM ....

Please write too the code how make the call to the function getCheckedValue.

Try in Jquery way ... $('#cat_id:checked').val(); Jquery is Rock :D

JavaScript function ...


function getCheckedValue(radioObj) {

  for ( var i in radioObj )if (radioObj[i].checked) return radioObj[i].value;

  return false;

}

Roger Russel
A: 

I would say the problem is the way you generate your radio buttons:

echo '<li><input type="radio" name="cat_id_' . $value['cat_id'] 
   . '" id="cat_id_' . $value['cat_id'] 
   . '" value="' . $value['cat_id'] . '" /> ' 
   . htmlentities($value['cat_title']) . '<br />';

Try to get jQuery to make your life simple. You could get your values like that:

$('#cat_ul input[type=radio]:checked').val();

(assuming your <li> tags are wrapped in a <ul id="cat_ul"> tag)

MarvinLabs