keeping radio button value after post

12.8k views Asked by At

HI

i'm using a php page and i need to keep the value of and check box and radio button (checked or not checked) after post page.

how could i make it?

thanks

4

There are 4 answers

0
Sarfraz On

Something like this:

<?php if (isset($_POST['checkbox_name']))?>
<input type="checkbox" checked="checked" value="<?php echo $_POST['checkbox_name'];?>" />
<?php} ?>

<?php if (isset($_POST['radio_name']))?>
<input type="radio" checked="checked" value="<?php echo $_POST['radio_name'];?>" />
<?php} ?>

What happens is that you check if the input variables are in the $_POST and if so you add checked="checked" to the input fields to make them checked.

0
Knowledge Craving On

You need something like:-

<?php
$postCheckboxName = '';
if (isset($_POST['checkbox_name']) || 'any_value' == $_POST['checkbox_name']) {
    $postCheckboxName = ' checked="checked"';
}
?>
<input type="checkbox" name="checkbox_name" value="any_value"<?php echo $postCheckboxName;?> />

<?php
$postRadioName = '';
if (isset($_POST['radio_name']) || 'any_other_value' == $_POST['radio_name']) {
    $postRadioName = ' checked="checked"';
}
?>
<input type="checkbox" name="radio_name" value="any_other_value"<?php echo $postRadioName;?> />

This code should get you going. I'm basically checking whether the POST value of either the checkbox / radio element is set or not & whether the corresponding element's value matches with my respective element's value or not.

Hope it helps.

0
Naveen On

First get the radio button value.

$radiobuttonvalue = $_POST['radiobuttoname']

Then for each radio button with the same name, do this

<input type="radio" name="radiobuttonname" value="value" id="radiobuttonname" <?php if($radiobuttonvalue == "value") { echo 'checked="checked"';} ?>
0
spiderman On

This worked for me, and is self explanatory

sample code usage:

<div class="form-group">
    <label class="radio-inline">
        <input type="radio" name="time" value="lunch" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='lunch' ){echo ' checked="checked"';}?>>Lunch</label>
    <label class="radio-inline">
        <input type="radio" name="time" value="dinner" <?php if (isset($_POST[ 'time']) && $_POST[ 'time']=='dinner' ){echo ' checked="checked"';}?>>Dinner</label>
</div>