douchuitang0331 2016-01-21 17:46
浏览 47
已采纳

如何在编辑表单中显示支票?

Basically I have a form that I start out with. On that form is the option to select a checkbox and once I click submit on that form, the information is read into a database and the condition on whether the checkbox has been selected now displays a '0' or '1' in the database.

Now I have an edit form. I choose the row I want to edit and my edit form gets populated with what is in the row. The only thing that doesn't show up, is whether that row was 'checked' on the first form. I want the checkbox to be marked if the user checked the checkbox on the first form and visversa.

Sorry if this sounds like a terrible explination.

Code for the first form:

<form method="post" action="processForm.php">

    Name: <input type="text" name="names" required = "required"><br>

    Activate: <input type="checkbox" name="activateBox"><br>

    <input type="submit" value="Create Users"><br>
</form>

And code for edit form:

<form method="post" action="editForm.php">

    ID: <input type="text" readonly name="id" value="<?php echo $row['id']; ?>"><br><br>
    First Name: <input type="text" name="firstName" value="<?php echo $row['firstName']; ?>"><br><br>
    Last Name: <input type="text" name="lastName" value="<?php echo $row['lastName']; ?>"><br><br>
    Email: <input type="text" name="email" value="<?php echo $row['email']; ?>"><br><br>
    Activate: <input type="checkbox" name="activateBox" value="<?php echo $row['activated']; ?>"><br><br>

    <input type="submit" value="Update"><br>

</form>

I tried doing this but it did nothing. I'm sure I'm missing something pretty simple but I can't figure out what.

if (isset($_POST['activateBox'])) {
    echo 'checked="checked"';
}
  • 写回答

2条回答 默认 最新

  • dtziv24262 2016-01-21 17:49
    关注

    The checked attribute of the <input type="checkbox"> tag should work. Most likely, when you loaded the edit form, it was an HTTP GET request, rather than a POST, so the $_POST['activateBox'] was empty.

    In any case, reading the current state on an edit form from the client seems wrong. Shouldn't the current state come from the database?

    Try this:

    Activate: <input type="checkbox" name="activateBox"<?php if ($row['activated']) echo " checked"; ?>>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系