Jump to content

Radio buttons staying selected.


facarroll

Recommended Posts

I have multiple radio buttons staying selected on a several forms in different scripts when they were working properly (only one selects at a time) yesterday. The scripts have not been altered. None of my scripts containing radio buttons are working as they should. I can select all of the buttons at once. Does anyone have a clue about what could be going on?

Link to comment
Share on other sites

Fair enough.

 

 
<form method="post" enctype="multipart/form-data" action="<?PHP echo $_SERVER["PHP_SELF"]; ?>">
    <tr>
          <input type="text" name="given" value="" /><br />Enter the Given Name
        <p>
          <input type="text" name="family" value="" /><br />Enter the Family Name
        <p>
        
          <input type="text" name="transferCode" value="" /><br />Enter the  Transfer Code
        <p>
          <input type="text" name="userCode" value="" /><br />Enter the User Password
        <p>
           <input type="radio" name="userGroup_1" value="<?php echo $group1; ?>" /><?php echo $group1; ?>
        <p>
          <input type="radio" name="userGroup_2" value="<?php echo $group2; ?>" /><?php echo $group2; ?>
        <p>
          <input type="radio" name="userGroup_3" value="<?php echo $group3; ?>" /><?php echo $group3; ?>
        <p>
          <input type="radio" name="userGroup_4" value="<?php echo $group4; ?>" /><?php echo $group4; ?>
        <p>
          <input type="radio" name="userGroup_5" value="<?php echo $group5; ?>" /><?php echo $group5; ?>
        <p>
          <input type="radio" name="userGroup_6" value="<?php echo $group6; ?>" /><?php echo $group6; ?>
        <p>
        Select the Allocated Student Group
        <p>
          <input type="submit" value="submit" name="Send Data">
  </form>

Link to comment
Share on other sites

I've figured out that the name field must be identical in each case, but accompanied by an id field like this.

 

<input type="radio" name="userGroup" id="1" value="<?php echo $group1; ?>" />
<input type="radio" name="userGroup" id="2" value="<?php echo $group1; ?>" />
<input type="radio" name="userGroup" id="3" value="<?php echo $group1; ?>" />

 

But can anyone tell me how the POST process should work in this case?

Link to comment
Share on other sites

shouldn't all the names of the radio buttons be the same?

 

<input type="radio" name="userGroup" value="<?php echo $group1; ?>" /><?php echo $group1; ?>

<input type="radio" name="userGroup" value="<?php echo $group2; ?>" /><?php echo $group2; ?>

<input type="radio" name="userGroup" value="<?php echo $group3; ?>" /><?php echo $group3; ?>

 

..and so forth

Link to comment
Share on other sites

dadamssg is correct. To create a group of radio buttons, you'd give them all the same name attribute. They would then each have different value attributes, and only the value from the one that is selected would be in the $_POST array.

 

<!-- group 1 -->
On: <input type="radio" name="first" value="on"><br>
Off: <input type="radio" name="first" value="off"><br>
<br>
<!-- group 2 -->
Green: <input type="radio" name="second" value="green"><br>
Blue: <input type="radio" name="second" value="blue"><br>
Yellow: <input type="radio" name="second" value="yellow"><br>

Link to comment
Share on other sites

  • 2 weeks later...

Hi,

I am new in this forum. I saw your question, and I registered just to answer it.

Instead of write this code:

<input type="radio" name="userGroup" id="1" value="<?php echo $group1; ?>" />

<input type="radio" name="userGroup" id="2" value="<?php echo $group1; ?>" />

<input type="radio" name="userGroup" id="3" value="<?php echo $group1; ?>" />

Write like this :

<?php

switch($_POST['userGroup']){

case '1': $group1= 'checked';

break;

case '2': $group2= 'checked';

break;

default: $group3=  'checked';

}

?>

<input type="radio" name="userGroup" id="1" value="1" <?php echo $group1; ?> />

<input type="radio" name="userGroup" id="2" value="2" <?php echo $group2; ?> />

<input type="radio" name="userGroup" id="3" value="3" <?php echo $group3; ?> />

 

Which means that group3 is checked by default. If you don't want any default checked, just put it in the case option like others. :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.