提交 PHP 后保留表单值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5514897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Keep form values after submit PHP
提问by shinjuo
I forgot to say there are drop down menus also that I would like to keep the chosen value of
我忘了说还有下拉菜单,我想保留选择的值
I have a form with checkboxes, a radio buttons, and a few text fields. I know how to keep the text field values after form submit, but I would like to keep the radio button selection and checkboxes checked after submit. I am posting to the same page.
我有一个带有复选框、单选按钮和一些文本字段的表单。我知道如何在表单提交后保留文本字段值,但我想在提交后保持单选按钮选择和复选框的选中状态。我发布到同一页面。
回答by Jon
To have the radio buttons and checkboxes pre-checked, you need to add the checked="checked"
attribute to the HTML you generate for each control you want to display checked.
要预先选中单选按钮和复选框,您需要将checked="checked"
属性添加到为每个要显示选中的控件生成的 HTML 中。
For example, if you currently have this:
例如,如果您目前有这个:
<input type="checkbox" name="foo" value="bar" />
You want to change it to this:
你想把它改成这样:
<input type="checkbox" name="foo" value="bar"
<?php echo empty($_POST['foo']) ? '' : ' checked="checked" '; ?>
/>
Update:For drop down menus, you want to change this:
更新:对于下拉菜单,您要更改此:
<select name="foo">
<option value="bar">Text</option>
</select>
To this, which uses selected="selected"
:
为此,它使用selected="selected"
:
<select name="foo">
<option value="bar"
<?php if(isset($_POST['foo']) && $_POST['foo'] == 'bar')
echo ' selected="selected"';
?>
>Text</option>
</select>
Be careful to keep the two "bar" values that appear above synchronized (echoing the options in a loop will help to make sure of that).
小心保持上面出现的两个“条”值同步(在循环中回显选项将有助于确保这一点)。
回答by xil3
You could do this:
你可以这样做:
<input name="cb" type="checkbox" <?php echo (isset($_POST['cb']) ? 'checked' : '') ?>>
回答by Chris Sobolewski
<input type="checkbox" name="foo" value="foo" <?php if(isset($_POST['foo'])){echo 'checked';} ?>"/>
回答by ashes999
Use the same paradigm that you use for text-boxes for other fields. You just need to set a different HTML property instead of passing some text through a variable.
使用与用于其他字段的文本框相同的范例。您只需要设置不同的 HTML 属性,而不是通过变量传递一些文本。
For both radio boxes and checkboxes, set the HTML property "CHECKED" and they will be checked.
对于单选框和复选框,设置 HTML 属性“CHECKED”,它们将被选中。
回答by Damb
<input type="text" name="nazev_projektu" id="nazev_projektu" class="inp" value="<?php if(isset($_POST['nazev_projektu'])) echo $_POST['nazev_projektu']; ?>" />
You can do the same thing with checked="checked" etc.
你可以用checked="checked"等做同样的事情。
<input type="checkbox" ... ="<?php if(isset($_POST['ThisRadioIsChecked'])) echo 'checked="checked"'; ?>" ... />