在提交PHP之后保持表单值

时间:2022-09-25 14:51:54

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.

我有一个带有复选框、单选按钮和一些文本字段的表单。我知道如何在提交表单后保留文本字段值,但是我想在提交后保留单选按钮和复选框。我在同一个页面上发帖。

5 个解决方案

#1


13  

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.

要预先选中单选按钮和复选框,需要将勾选="勾选"属性添加到要为每个控件生成的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=“选中”:

<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).

要注意保持上面出现的两个“bar”值保持同步(在循环中重复选项将有助于确保同步)。

#2


3  

You could do this:

你可以这样做:

<input name="cb" type="checkbox" <?php echo (isset($_POST['cb']) ? 'checked' : '') ?>>

#3


3  

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

#4


1  

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”,它们将被选中。

#5


0  

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

你也可以用check ="checked"等来做同样的事情。

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

#1


13  

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.

要预先选中单选按钮和复选框,需要将勾选="勾选"属性添加到要为每个控件生成的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=“选中”:

<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).

要注意保持上面出现的两个“bar”值保持同步(在循环中重复选项将有助于确保同步)。

#2


3  

You could do this:

你可以这样做:

<input name="cb" type="checkbox" <?php echo (isset($_POST['cb']) ? 'checked' : '') ?>>

#3


3  

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

#4


1  

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”,它们将被选中。

#5


0  

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

你也可以用check ="checked"等来做同样的事情。

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