【问题标题】:Display a checked checkbox in a form to form POST submission在表单中显示选中的复选框以形成 POST 提交
【发布时间】:2014-03-24 23:40:57
【问题描述】:

我有这个向导需要显示之前发布的值

index.html

<form method="post" action="posted.php">
<input type="text" name="surname" value="" placeholder="Surname" />
<input type="text" name="firstname" value="" placeholder="Firstname" />
<input type="checkbox" name="php" />
<input type="checkbox" name="jquery" />
<input type="checkbox" name="python" />
<input type="submit" value="Submit" />
</form>

posted.php 中我有一个类似的表格,只是这次我知道$_POST 的值

   <form method="post" action="finish.php">
    <input type="text" name="surname" value="<?php echo $_POST['surname']; ?>" placeholder="Surname" />
    <input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" placeholder="Firstname" />
    <input type="checkbox" name="php" />
    <input type="checkbox" name="jquery" />
    <input type="checkbox" name="python" />
    <input type="submit" value="Submit" />
    </form>

我很难想出一个解决方案来显示选中的复选框。我见过几个解决方案,比如https://stackoverflow.com/a/11424091/1411148,但我想知道在 html5 或 jquery 中是否有更多解决方案。

如何显示选中的复选框?。我遇到的问题是 &lt;input type="checkbox" name="jquery" checked /&gt; checked 选中了复选框,并且无法添加 post 数据来显示用户选中的内容。

【问题讨论】:

  • 如果你给他们一个值,你可以使用 php 在输入标签上回显“checked”属性。 &lt;?= ($_POST['jquery'] == '[VALUE'])? "checked" : ""; ?&gt;。只有在检查表单提交时,该值才会通过。

标签: php jquery forms checkbox


【解决方案1】:

这是一条可行的路:

<form method="post" action="finish.php">
    <input type="text" name="surname" value="<?php echo $_POST['surname']; ?>" placeholder="Surname" />
    <input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" placeholder="Firstname" />
    <input type="checkbox" name="php" <?php if (isset($_POST['php'])) echo 'checked="checked"'; ?> />
    <input type="checkbox" name="jquery" <?php if (isset($_POST['jquery'])) echo 'checked="checked"'; ?> />
    <input type="checkbox" name="python" <?php if (isset($_POST['python'])) echo 'checked="checked"'; ?> />
    <input type="submit" value="Submit" />
</form>

【讨论】:

    猜你喜欢
    • 2013-03-13
    • 1970-01-01
    • 2023-02-17
    • 2012-05-23
    • 2021-09-22
    • 2021-10-29
    • 2013-08-04
    相关资源
    最近更新 更多