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 9 of 9
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Enable/Disable Radio-Buttons using only PHP?

    I am trying to combine 3 pages which have radio buttons into one page.

    Two of the pages will need to be Read/Write as far as the Radio Buttons go, but the 3rd page should be read-only and simply display what value is set in the database.

    I need a way to make the radio buttons either "disabled" or "read-only" when the 3rd page/view is displayed.

    How can I do using using *only* HTML and PHP?

    Thanks,


    Debbie

  • #2
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    334
    Thanks
    2
    Thanked 48 Times in 47 Posts
    PHP Code:
    <input type='radio' name='radiol' value='blahblah' 
    <?php if(condition that checks whether 3rd page is viewed/displayed)

    echo 
    disabled='disabled';

    />
    Last edited by Redcoder; 01-27-2013 at 05:13 PM.

  • #3
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Redcoder View Post
    PHP Code:
    <input type='radio' name='radiol' value='blahblah' 
    <?php if(condition that checks whether 3rd page is viewed/displayed)

    echo 
    disabled='disabled';

    />
    Is your solution above supported by *all* Browsers?



    ----
    Let me back up, and explain a little more of what I'm trying to do...

    So currently, I have 3 separate pages...

    - Manage Friend-Requests (New)
    - Manage Friend-Requests (Declined by Me)
    - View Friend-Requests (Made by Me)


    The first two pages are nearly identical, except a slightly different query and different labels.

    The radio buttons on each of these pages needs to be Read/Write, because the logged in User that may either want to see (i.e. Read) what types of Friend-Requests he/she has, or the User may want to change (i.e. Write) one of the Friend Requests (e.g. Take an Incoming Friend-Request and Decline it, or take a Declined Friend-Request and make the person a Friend).


    The 3rd page is also similar to the first two pages, except that it too has a different query, AND this page is for information purposes only, so it either needs to be DISABLED or made READ-ONLY, even though there won't be a "Submit" button.

    On the 3rd page, the user would see one of three radio buttons checked (e.g. "Decide Later", "Accept" or "Decline") which denotes what the Member they sent a Friend-Request to decided to do. So, of course, there should be absolutely no way for the "Requestor" to modify the Form, or to hack things on the back-end to change values in the database, because it is the "Requestee" that is the one who makes the decision about the Friend-Request if you follow me?

    So, what is the best way to accomplish what I am describing above?

    Should I "Disable" the radio buttons on the 3rd page?

    Or should I make them "read-only"?


    Sincerely,


    Debbie

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Once you have presented a page, with a form etc., to the user you have lost control over it. The user can muck around with it: enable radio buttons - do whatever they want (if they know how).

    The important thing is that you ignore or discard any return (posted) values that you are not expecting, and check that the values you are expecting are acceptable.

    disabled="disabled" is how we disable radio buttons - or you can hide them or don't include them.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #5
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by AndrewGSW View Post
    Once you have presented a page, with a form etc., to the user you have lost control over it. The user can muck around with it: enable radio buttons - do whatever they want (if they know how).

    The important thing is that you ignore or discard any return (posted) values that you are not expecting, and check that the values you are expecting are acceptable.

    disabled="disabled" is how we disable radio buttons - or you can hide them or don't include them.
    I am hating myself for trying to combine 3 scripts into one...

    Here is what I have so far, and it is not working...
    PHP Code:
    <?php
        
    // ************************
        // Loop through Requests.    *
        // ************************
        
    while (mysqli_stmt_fetch($stmt3)){
            
    // **************************************
            // Build Friend-Request Decision array.    *
            // **************************************
            
    $friendRequestDecision[$requestorID] = (isset($requesteeApproved) ? $requesteeApproved 0);


            
    // **************************
            // Display Friend-Requests.    *
            // **************************
            
    echo "<li>
                    <a href='/account/profile/$username/about-me'>
                        $username<br />
                        <img src='/uploads/"
                            
    getSafeUserPhoto($photoName$photoApproved) .
                            
    "' width='80' alt='Thumbnail of " $username "' />
                    </a>

                    <fieldset id='requestChoices'>

                    <input id='Requestor" 
    $requestorID "_1' name='friendRequestDecision["
                    
    $requestorID "]' type='hidden' "
                    
    . ((isset($friendRequestDecision[$requestorID]) && $friendRequestDecision[$requestorID] == '0') ? "value='0'" "") .
                    
    " />

                    <input id='Requestor" 
    $requestorID "_1' name='friendRequestDecision["
                    
    $requestorID "]' type='radio' value='0' " 
                    
    . ((isset($friendRequestDecision[$requestorID]) && $friendRequestDecision[$requestorID] == '0') ? "checked='checked'" "") .
                    
    " disabled='disabled' />

                    <label for='Requestor" 
    $requestorID "_1'>Decide Later</label>

                    <input id='Requestor" 
    $requestorID "_2' name='friendRequestDecision["
                    
    $requestorID "]' type='hidden' "
                    
    . ((isset($friendRequestDecision[$requestorID]) && $friendRequestDecision[$requestorID] == '1') ? "value='1'" "") .
                    
    " />

                    <input id='Requestor" 
    $requestorID "_2' name='friendRequestDecision["
                    
    $requestorID "]' type='radio' value='1' " 
                    
    . ((isset($friendRequestDecision[$requestorID]) && $friendRequestDecision[$requestorID] == '1') ? "checked='checked'" "") .
                    
    " disabled='disabled' />

                    <label for='Requestor" 
    $requestorID "_2'>Accept</label>

                    <input id='Requestor" 
    $requestorID "_3' name='friendRequestDecision["
                    
    $requestorID "]' type='hidden' "
                    
    . ((isset($friendRequestDecision[$requestorID]) && $friendRequestDecision[$requestorID] == '2') ? "value='2'" "") .
                    
    " />

                    <input id='Requestor" 
    $requestorID "_3' name='friendRequestDecision["
                    
    $requestorID "]' type='radio' value='2' "
                    
    . ((isset($friendRequestDecision[$requestorID]) && $friendRequestDecision[$requestorID] == '2') ? "checked='checked'" "") .
                    
    " disabled='disabled' />

                    <label for='Requestor" 
    $requestorID "_3'>Decline</label>

                </fieldset>
            </li>\n"
    ;

        }
    //End of LOOP THROUGH REQUESTS
    ?>

    If I just load the form, it seems to be working okay, but if I re-submit the form without changing any values, then I get behavior like this...

    Test #1:
    There are two Friend-Requests:
    - The one to "Mary" is originally marked as "Accept"
    - The one to "Sally" is originally marked as "Accept"


    When I re-submit the form...
    - The one to "Mary" changes to "Decide Later"
    - The one to "Sally" changes to "Decide Later"



    Test #2:
    There are two Friend-Requests:
    - The one to "Mary" is originally marked as "Decline"
    - The one to "Sally" is originally marked as "Decline"


    When I re-submit the form...
    - The one to "Mary" remains as "Decline"
    - The one to "Sally" remains as "Decline"



    Test #3:
    There are two Friend-Requests:
    - The one to "Mary" is originally marked as "Decide Later"
    - The one to "Sally" is originally marked as "Decide Later"


    When I re-submit the form...
    - The one to "Mary" remains as "Decide Later"
    - The one to "Sally" remains as "Decide Later"


    I'm not sure what is wrong with my code?!

    BTW, obviously I would want to hide the "Update Requests" button for this particular view, but I am leaving it for testing purposes. My logic being, that my code should be able to properly handle things if the Form was somehow re-submitted, which I sorta think plays to the points you were trying to make above about not being able to trust Form data, right??

    Sincerely,


    Debbie

  • #6
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    The first thing I would do would be to get rid of all this unnecessary repetition:

    PHP Code:
    (isset($friendRequestDecision[$requestorID]) && $friendRequestDecision[$requestorID] == '0'
    You've checked this value already:

    PHP Code:
    $friendRequestDecision[$requestorID] = (isset($requesteeApproved) ? $requesteeApproved 0); 
    Just assign this value to a temporary (shorter) variable for use within the loop:
    PHP Code:
    $decision $friendRequestDecision[$requestorID]; 
    This will make the code much easier to read.

    I would also use HEREDOCs to reduce the clutter:

    PHP Code:
    $an_input = <<< HEREDOC
    <input id='Requestor{$requestorID}{$ID}' name='{$requestorID}' type='radio' 
    etc.. >
    HEREDOC;

    // then later 
    echo $an_input
    And, yes, put whatever you need on the page initially to help you test and debug it

    Added: I know that I haven't answered your previous question directly but.. if you make these changes it will make it much easier for you (everyone) to read and understand, and to discover what any issues are.
    Last edited by AndrewGSW; 01-27-2013 at 11:31 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #7
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    I would also use code like the following:

    PHP Code:
        $friendRequestDecision[$requestorID] = (isset($requesteeApproved) ? $requesteeApproved 0); 
        
    $decision intval($friendRequestDecision[$requestorID], 10); // convert to a number/integer

        
    $decision_vals = array();
        
    $decision_checked = array();
        for (
    $i 0$i 3$i++) {
            if (
    $decision[$i] == $i) {
                
    $decision_vals[] = "value='$i' ";
                
    $decision_checked[] = "checked='checked' ";
            } else {
                
    $decision_vals[] = "";
                
    $decision_checked[] = "";
            }
        } 
    Actually, all of your input-creation could possibly be put in this loop as there is a lot of repetition.

    ..with code such as:

    PHP Code:
        <input id='Requestor{$requestorID}_1' name='friendRequestDecision[" 
        . $requestorID . "]' 
    type='hidden' {$decision_vals[$i]} /> 
    Last edited by AndrewGSW; 01-28-2013 at 12:01 AM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #8
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts
    AndrewGSW,

    Thanks for trying to help streamline my code, but before doing that it makes sense to just get it working...

    I don't see where you answered the questions I had in my last post with test data.

    Without understanding what is wrong with my current code, the other suggestion only further confuse me.

    Sincerely,


    Debbie

  • #9
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Here is a sample of my code when I do View Source which should be easier to read...

    PHP Code:
        <fieldset id='requestChoices'>
            <
    input id='Requestor38_1' name='friendRequestDecision[38]' type='hidden'  />
            <
    input id='Requestor38_1' name='friendRequestDecision[38]' type='radio' value='0'  disabled='disabled' />
            <
    label for='Requestor38_1'>Decide Later</label>

            <
    input id='Requestor38_2' name='friendRequestDecision[38]' type='hidden' value='1' />
            <
    input id='Requestor38_2' name='friendRequestDecision[38]' type='radio' value='1' checked='checked' disabled='disabled' />
            <
    label for='Requestor38_2'>Accept</label>

            <
    input id='Requestor38_3' name='friendRequestDecision[38]' type='hidden'  />
            <
    input id='Requestor38_3' name='friendRequestDecision[38]' type='radio' value='2'  disabled='disabled' />
            <
    label for='Requestor38_3'>Decline</label>
        </
    fieldset
    Hope that helps some?!


    Debbie


  •  

    Posting Permissions

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