Daanvn
Daanvn

Reputation: 1266

Dynamically created radio button issue

I have a form where I can subscribe and unsubscribe users to a newsletter, the 'form' itself looks like this:

enter image description here

The checkboxes are dynamically created with this code:

<?php 
$i = 0;  
while($objResult1 = mysql_fetch_array($objQuery1))  
 {  
$i++;  
?>  
<tr>  
   <td><div align="center"><?=$objResult1["ID"];?><input type="hidden" name="mailid[]" value="<?=$objResult1["ID"];?>"> </div></td>
   <td><div align="center"><?=$objResult1["Titel"];?> </div></td>  
   <td><div align="center"><input type="checkbox" name="sub[]" value="10"> </div></td>  
   <td><div align="center"><input type="checkbox" name="sub[]" value="90"> </div></td>
</tr>  
<?php  
 }  
?>  

I use the values in another script where I insert them into a database like this:

foreach($_POST['sub'] as $i=>$s){
$sql = mysql_query("INSERT INTO Subscriptions (Klant_ID, Mail_ID, Status, Datum) VALUES   ('".$Klant_ID."', '".$_POST['mailid'][$i]."', '".$s."', '".$Datum."')") or die(mysql_error());
}

The problem however is that it is possible to select both checkboxes in every row (basicly I can select all checkboxes). When I set the input type to "radio" I can only select 1 of the 6 radio buttons. Is there any way I can get this to work so I can select 1 radio button on each row? If you have any questions or you need more info just ask in the comments. Thnx in advance!

NOTE: There are not always 3 rows, the rows are dynamically created depending on how many rows there are in the database.

EDIT: I got it to work with this code thanks to Jueecy's answer:

<?php for ($i = 0; $objResult1 = mysql_fetch_array($objQuery1); $i++) : ?>
<tr>  
<td><div align="center"><?=$objResult1["ID"];?><input type="hidden" name="mailid[]"     value="<?=$objResult1["ID"];?>"> </div></td>
<td><div align="center"><?=$objResult1["Titel"];?> </div></td>  
<td><div align="center"><input type="radio" name="sub[<?php echo $i; ?>]" value="10"> </div></td>  
<td><div align="center"><input type="radio" name="sub[<?php echo $i; ?>]" value="90"> </div></td>
<tr>
<?php endfor; ?>

I only have 1 other problem now, the row with the ID 1 isn't showing anymore. Anyone knows how to fix this?^^

Upvotes: 1

Views: 3018

Answers (5)

Nisarg
Nisarg

Reputation: 3252

You can use this code : It works 100%

<tr>  
            <td><div align="center">1</div></td>
            <td><div align="center">2</div></td>  
            <td><div align="center"><input type="radio" name="sub[1]" value="10"> </div></td>  
            <td><div align="center"><input type="radio" name="sub[1]" value="90"> </div></td>
        </tr>  
        <tr>  
            <td><div align="center">1</div></td>
            <td><div align="center">2</div></td>  
            <td><div align="center"><input type="radio" name="sub[2]" value="10"> </div></td>  
            <td><div align="center"><input type="radio" name="sub[2]" value="90"> </div></td>
        </tr>  
        <tr>  
            <td><div align="center">1</div></td>
            <td><div align="center">2</div></td>  
            <td><div align="center"><input type="radio" name="sub[3]" value="10"> </div></td>  
            <td><div align="center"><input type="radio" name="sub[3]" value="90"> </div></td>
        </tr> 

Upvotes: 0

Shoe
Shoe

Reputation: 76240

Is there any way I can get this to work so I can select 1 radio button on each row?

Of course there is. You should choose to have radios buttons and group them by name. Only radio buttons with the same name will be restricted to have only one value:

<input type="radio" name="row1" value="x">X</input>
<input type="radio" name="row1" value="y">Y</input>

<input type="radio" name="row2" value="x">X</input>
<input type="radio" name="row2" value="y">Y</input>

I think you can easily come up with a proper loop for that. Something along the lines of:

<?php for ($i = 0; $objResult1 = mysql_fetch_array($objQuery1); $i++) : ?>
<tr>  
   <td><div align="center"><?=$objResult1["ID"];?><input type="hidden" name="mailid[]" value="<?=$objResult1["ID"];?>"> </div></td>
   <td><div align="center"><?=$objResult1["Titel"];?> </div></td>  
   <td><div align="center"><input type="checkbox" name="sub<?php echo $i; ?>" value="10"> </div></td>  
   <td><div align="center"><input type="checkbox" name="sub<?php echo $i; ?>" value="90"> </div></td>
<tr>
<?php endfor; ?>

Upvotes: 1

Jonas &#196;ppelgran
Jonas &#196;ppelgran

Reputation: 2747

Either use one radio button group per row or use javascript to deselect unsubscribe when selecting subscribe and vice versa.

Upvotes: 0

Xavier S.
Xavier S.

Reputation: 1157

i give an example for a radio button:

    <html>
    <head>
    <title>My Page</title>
    </head>
    <body>
    <form name="myform" action="http://www.mydomain.com/myformhandler.cgi" method="POST">
    <div align="center"><br>
    <input type="radio" name="group1" value="Milk" /> Milk<br />
    <input type="radio" name="group1" value="Butter" checked /> Butter<br />
    <input type="radio" name="group1" value="Cheese" /> Cheese
    <hr />
    <input type="radio" name="group2" value="Water"/> Water<br />
    <input type="radio" name="group2" value="Beer" /> Beer<br />
    <input type="radio" name="group2" value="Wine" checked /> Wine<br />
    </div>
    </form>
    </body>
    </html>

To group radioboxes use "name" attribute. I just change your example as following:

    <?php 
    $i = 0;  
    while($objResult1 = mysql_fetch_array($objQuery1))  
     {  
    $i++;  
    ?>  
    <tr>  
       <td><div align="center"><?=$objResult1["ID"];?><input type="hidden" name="mailid[]" value="<?=$objResult1["ID"];?>"> </div></td>
       <td><div align="center"><?=$objResult1["Titel"];?> </div></td>  
       <td><div align="center"><input type="checkbox" name="sub1" value="10"> </div></td>  
       <td><div align="center"><input type="checkbox" name="sub1" value="90"> </div></td>

       <td><div align="center"><input type="checkbox" name="sub2" value="10"> </div></td>  
       <td><div align="center"><input type="checkbox" name="sub2" value="90"> </div></td>
    </tr>  
    <?php  
     }  
    ?>  

Upvotes: 0

Peter Herdenborg
Peter Herdenborg

Reputation: 5962

For that to be possible, the name of the radio buttons must only be shared per row, since the name defines the group of radios of which only one can be checked.

Fixing this obviously has implications for the rest of your solution, but I'm sure you can work around those.

Upvotes: 0

Related Questions