开发者

keeping radio button value after post

开发者 https://www.devze.com 2023-01-10 19:38 出处:网络
HI i\'m using a php pa开发者_开发技巧ge and i need to keep the value of and check box and radio button(checked or not checked) after post page.

HI

i'm using a php pa开发者_开发技巧ge 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


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"';} ?>


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.


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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消