Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2004
    Posts
    95
    Thanks
    0
    Thanked 0 Times in 0 Posts

    selecting a radio button...

    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:

    Code:
    function getstate() {
      document.accounts.states.selectedIndex = <?php echo $_POST['states']; ?>;
      document.accounts.admin[<?php echo $_POST['admin']; ?>].checked = true;
    }
    Code:
    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.

    -Thanks,
    Rich

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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...

    Try
    document.accounts.admin[Number(<?php echo $_POST['admin']; ?>)].checked = true;
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,032
    Thanks
    0
    Thanked 248 Times in 245 Posts
    If $_POST['admin'] contains the value of the selected radio button, then the code must be like this:
    Code:
    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;
          break;
        }
      }
    }
    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.

    PHP Code:
    <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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •