I need to have radio buttons remain selected and still be able to use pagination. Let me elaborate.

I am using PHP 4.2.2 to code an administration page that lists users and allows an admin to choose a pair of users so that files may be copied from one user to another. The users are chosen via radio buttons and a form is used to submit the users to another page where files are chosen and the copy is executed. I am also using pagination, sessions and output buffers. Both pagination and the radio buttons are working great independently. However, if I select one user on one page and the second user on another page the selection of the first user is lost. If both users are selected on the same page the form works as expected.

Can I get radio buttons and pagination to work using PHP? If so how?

Do I have to use JavaScript? I've seen post on the net suggesting JavaScript but I can't get it to work either. I've tried setting the radio button to "true" directly and using a function:

Directly

<input type="radio" name="from_user" value="'.$row['user_id'].'" 
onChange="document.ChooseUsers.from_user.checked=\'true\'"> 

and with a function

<SCRIPT LANGUAGE="JavaScript">
<!-- Beginning of JavaScript -
function FromUser (textstring) 
{
ChooseUsers.from_user.checked='textstring'
}
  // - End of JavaScript - -->
</script>
<input type="radio" name="from_user" value="'.$row['user_id'].'"
 onChange="FromUser(true)">

Thanks for your time

    Maybe I misunderstand the question, but it seems to me that you just need to make sure that you don't throw out the data from the previous page. In other words, moving between pages should be a form submit operation, POST or GET-wise. Each page, in addition to the (visible) radio buttons should also have a number of hidden elements corresponding to the values of copy_from and copy_to. When I go to page 2 from page 1, I'm really submitting page 1 to page 2. All page 2 needs to do with that data, then, is make sure it gets in the right boxes. Something like this:

    print "<input type=hidden name=\"copy_from\" value=\"" . $_REQUEST['copy_from'] . "\">\n";

    And the same for copy_to. Combine that with your JavaScript, below, call the radio buttons something else (so they don't conflict with the hidden copy_from and copy_to, which will be doing the real work), and make your "Next Page" and "Previous Page" links into form submissions, and all should go well.

      saintp,

      Thanks for your suggestions. If I understand the basic idea you are suggesting two forms. The original form that sends the two users to the script that does the actual file coping. And a second form (TransferUsers) that transfers the hidden boxes between the original paginated pages.

      So the pagination links will become something like

       <a href="javascript:document.TransferUsers.submit();" 
      onClick="goto=list_users.php? -pagination parameters- '">Next</a>
       

      I'll play around with it and hopefully post the pertinent working parts.

        11 days later

        Thanks to saintp for pointing me in the right direction for passing "checked" radio buttons in pagination links. The method is to use hidden input objects in the form. The inputs are set by a JavaScript function when the user clicks a radio button. The pagination links use href="java script:location.href= .... +document.forms[0].hiddenElement.value. to pass the stored values.
        Please see below for an example, full script on request.

        // In the form that contains the radio buttons
        // holds the copy_from user id
        echo "<input type=\"hidden\" name=\"copy_from\" value=\"" . $_GET['cf']. "\">\n";  
        // holds the copy_to user id echo "<input type=\"hidden\" name=\"copy_to\" value=\"" . $_GET['ct']. "\"> \n"; // Radio buttons <input type="radio" name="from_user" value="'.$row['user_id'].'" onChange="FromUser(this);"> <input type="radio" name="to_user" value="'.$row['user_id'].'" onChange="ToUser(this);"> // Sample pagination link $pagination .= ' <a href="java script:location.href='list_users.php?o='.$odr.'&prev_u='.$prev_u.' ...more pagination parameters... '&cf='+document.forms[0].copy_from.value + '&ct='+document.forms[0].copy_to.value">Next</a>'; // The FromUser and ToUser functions <script type="text/javascript"> <!-- Beginning of JavaScript - function FromUser (form) { var copyFrom = form.value document.forms['users'].copy_from.value=copyFrom // The hidden copy_from input } function ToUser (form) { var copyTo = form.value document.forms['users'].copy_to.value=copyTo //The hidden copy_to input } </script>

        I am sure there is more elegant way to do this but,I hope this helps someone

          Write a Reply...