dtf54486 2014-07-21 12:47
浏览 8
已采纳

PHP处理复选框

Im aware that this problem of mine could be resolved by using javasrcipt but in this case its not an option, PHP only.

There is a search form like this:

<form method="post" action="">
    <input type="checkbox" name="username">
    <input type="checkbox" name="email">
    <input type="checkbox" name="name">

    //irrelevant text inputs

    <input type="submit" value="submit">
</form>

Now if user decide to check username and name - script must keep it checked after submit, we could accomplish this easly in following way:

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

This may not seem desirable in this simple example but in real code there is around 30 different checkboxes so we need to specify default selection for most conducted searches, also easly doable this way:

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

At this point following problem appears:

if something has been checked by default and user unchecked it and submited form it will again check itselfe due to that } else { echo 'checked'; } conditio

unfortunetely submitting unchecked checkbox doesnt create empty key in $_POST array therefore i dont see how to approach this issue.

What im trying to accomplish is to:

  • set some of checkboxes to default check state
  • keep pre-submit checkboxes state to "after submit" (so they wont go back to opposite state)

lets make some visual representation of my problem:

  • user enters a page:

    ☑ username ☑ email ☒ name

  • user selects desirable checkboxes:

    ☒ username ☑ email ☑ name

  • user submits form and after submit it still looks the same way:

    ☒ username ☑ email ☑ name

  • 写回答

2条回答 默认 最新

  • dongwo5686 2014-07-21 14:43
    关注

    Give your input[type="submit"] a name, you can use that to determine whether or not the form was submitted:

    <input type="submit" name="submitted" value="submit" />
    

    Then you can use that as part of the condition for checking the checkboxes:

    <input type="checkbox" name="username"<?php echo !isset($_POST['submitted']) || isset($_POST['username']) ? ' checked' : ''; ?> ... />
    

    Alternatively, you can check $_SERVER['HTTP_REQUEST_METHOD'] to determine whether or not the form was posted.

    <input ... <?php echo $_SERVER['REQUEST_METHOD'] != 'POST' || isset($_POST['username']) ? ' checked' : ''; ?> ... />
    

    Here's an example

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab