1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Rep Power

    Retrieving values from radio buttons

    Hi friends,

    I am trying to develop Online Test Project.

    In this i have radio buttons to select an answers.For radio buttons i used the following code

    echo "<br>$n."."{$row['tmry_qun']} <br><input type='radio' name='group.$i+1' value='1'/>"."{$row['tmry_option_1']} <br><input type='radio' name='group.$i+1' value='2'/>"."{$row['tmry_option_2']} <br><input type='radio' name='group.$i+1' value='3'/>"."{$row['tmry_option_3']} <br><input type='radio' name='group.$i+1' value='4'/>"."{$row['tmry_option_4']} <br><input type='radio' name='group.$i+1' value='5'/>"."{$row['tmry_option_5']} <br>";

    the name of radio button I used is group.$i+1

    Now I want to access the value of selected radio button.

    Please suggest me to resolve this problem.

    Thank in advance,

    Thank you.
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Rep Power

    that looks ... weird.

    Before you can start fetching the values, you need to get your string syntax right.

    What's the point of randomly splitting the string into smaller parts and concatenating them? Remove that. You cannot do "complex" operations like $i + 1 within a string. You need to use concatenation instead.

    You also seem to have issues structuring your values. Numbered variables/identifiers like "tmry_option_1", "tmry_option_2", ..., "tmry_option_5135" or "group_1", "group_2", ... are almost always a bad sign. Use arrays instead of a bunch of loose variables.

    However, I'm not really sure if you need all that. If you only got a group of radio buttons, why not give it a constant name? Or do you have multiple groups?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Super Moderator
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2009
    Hartford, WI
    Rep Power
    Howdy. I am not sure what you are attempting to accomplish as far as radio button names, but if these 5 options are to 1 thing, then all names must match. This may be something more to your goal. I did move all your 'tmry_option' items to an inner array. If this is submitted via POST, then it would be referenced via $_POST['radioChoice']

    PHP Code:
    echo $row['tmry_qun'] . '<br />
    <input type="radio" name="radioChoice" value="1" />' 
    $row['tmry_option'][0] . '<br />
    <input type="radio" name="radioChoice" value="2" />' 
    $row['tmry_option'][1] . '<br />
    <input type="radio" name="radioChoice" value="3" />' 
    $row['tmry_option'][2] . '<br />
    <input type="radio" name="radioChoice" value="4" />' 
    $row['tmry_option'][3] . '<br />
    <input type="radio" name="radioChoice" value="5" />' 
    $row['tmry_option'][4] . '<br />'

IMN logo majestic logo threadwatch logo seochat tools logo