如果提交了表单,则选中复选框

时间:2014-12-02 03:46:52

标签: php html

我想这样做,如果我的表单已提交,那么应该选中复选框。

这就是我所拥有的:

<input type="checkbox" id="chk" <?php if ($_POST['submit']){ echo checked } ?> />

HTML:

<form id="form1" enctype="multipart/form-data" action="" method="post">
    //Stuff here
    <input name="submit" type="submit" value="Upload" />
</form>

我在测试时得到一个空白页...任何帮助都将受到赞赏。

2 个答案:

答案 0 :(得分:3)

空白是因为您的PHP代码存在php错误而您已关闭error_reporting

  1. if($_POST['submit'])应为if (isset($_POST['submit']))

  2. echo checked应为echo 'checked';

  3. 以下示例适用于我。

    <form id="form1" enctype="multipart/form-data" action="" method="post">
        //Stuff here
        <input name="submit" type="submit" value="Upload" />
    </form>
    <input type="checkbox" id="chk" <?php if (isset($_POST['submit'])){ echo 'checked'; } ?> />
    

答案 1 :(得分:0)

您的PHP代码有错误。请更正。

 <input type="checkbox" id="chk" <?php if ($_POST['submit']){ echo 'checked'; } ?> />
相关问题