Reputation: 147
I have a table, that generates rows dynamically. I have used radio buttons for each row. I want the radio button to be checked even after I click my submit button. This is body of my table:
<form action="" method="POST">
<table>
<thead>
<tr>
<td></td>
<td><h3>Date<h3></td>
<td><h3>Status<h3></td>
</tr>
</thead>
<tbody>
<?php for($i=$start;$i<$end;$i++)
{
$add=$ARRAY[$i]['source_address'];
$Status=$ARRAY[$i]['Status'];
$total=$add.$Status;
?>
<tr>
<td><input type="radio" name="ID[]" value="<?php echo $total; ?>"/></td>
<?php
echo'<td>'.$ARRAY[$i]['escl_date'].'</td>';
echo'<td>'.$ARRAY[$i]['escl_status'].'</td>';
?>
</tr>
<?php }?>
</tbody>
</table>
<input type="submit" name="details" value="details" />
How Can I make the radio button selected even after clicking on submit button? Please help me.
Upvotes: 0
Views: 85
Reputation: 22711
You can try this, You need a condition to check with reference value and add checked
attribute.
Here your reference value is your POST of ID
value. Add this into your radio
tag <?php echo $_POST['ID'][0]==$total ? 'checked':'';?>
<input type="radio" name="ID[]" value="<?php echo $total; ?>" <?php echo $_POST['ID'][0]==$total ? 'checked':'';?> />
Upvotes: 2
Reputation: 1415
The correct HTML attribute you are looking for is checked
.
Note to other answer: readonly
is not necessary, nor is it what OP is asking for.
Since you're doing it in PHP, let's say you're passing something with an HTML markup name
of foobar
, as follows:
<form><input type="radio" name="foobar"></form>
To make sure that the value is retained even after it is submitted, you could add a code like this (get
or post
depending on your purpose):
<form><input type="radio" name="foobar" <?php if(isset($_GET[foobar])){ echo "checked"; ?>></form>
This is to make sure that it would be checked if the name was passed in the form.
Note that this is just an example.
Upvotes: 1