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

报告相同问题?

悬赏问题

  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决