View Full Version : selecting a radio button...

01-23-2005, 02:53 AM
I already have a function created to set the focus of a combo box to the previous selection. But, now I have no clue on how to select the previous selected radio button. So lets say the user selects customer and hits submit and the pages shows up again I need the customer radio button automatically selected:

function getstate() {
document.accounts.states.selectedIndex = <?php echo $_POST['states']; ?>;
document.accounts.admin[<?php echo $_POST['admin']; ?>].checked = true;

document.accounts.admin[<?php echo $_POST['admin']; ?>].checked = true; is not working correctly. How would get this to work because it's causing my function to work improperly.


01-26-2005, 09:51 AM
hm... I guess that, as the querry send/received from server is a string, you may try transforming it in a number...as the index of the radio button is to be a number, not a string...

document.accounts.admin[Number(<?php echo $_POST['admin']; ?>)].checked = true;

01-26-2005, 10:24 AM
If $_POST['admin'] contains the value of the selected radio button, then the code must be like this:

function getstate() {
var f = document.accounts;
f.states.selectedIndex = <?php echo $_POST['states']; ?>;
for (var i=0; i<f.admin.length;i++){
if (f.admin[i].value == "<?php echo $_POST['admin']; ?>"){
f.admin[i].checked = true;

But if you will just call getstate() on onload of the page, then it's better to let php generate the markup that will set a radio option as the checked item. It's better because you don't rely on javascript (which can be disabled) to set the checked item.

<input type="radio" name="admin"<? if ($_POST['admin'] == "1") echo " selected=\"selected\""; ?> value="1" />Admin
<input type="radio" name="admin"<? if ($_POST['admin'] == "0") echo " selected=\"selected\""; ?> value="0" />Not Admin
I'm not a php coder so there might be syntax error in the code. :p

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum