php 如何在php中提交表单之前在html表单的复选框中设置默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36646411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:52:07  来源:igfitidea点击:

How to set default value in checkbox of html form before submitted form in php

php

提问by Pabari Mohit

Form :

形式 :

<tr> 
    <td>User Registration<input type="hidden" value="1" id="page[]" name="page[]"></td>
    <td><input type="checkbox" id="admin[]" name="admin[]" value="1" ~~CHECKED-ADMIN1~~></td>
    <td><input type="checkbox" id="staff[]" name="staff[]" value="1" ~~CHECKED-STAFF1~~></td>
    <td><input type="checkbox" id="member[]" name="member[]" value="1" ~~CHECKED-MEMBER1~~></td>
</tr>

<tr>
    <td>Member Registration<input type="hidden" value="2" id="page[]" name="page[]"></td>
    <td><input type="checkbox" id="admin[]" name="admin[]" value="1" ~~CHECKED-ADMIN2~~></td>
    <td><input type="checkbox" id="staff[]" name="staff[]" value="1" ~~CHECKED-STAFF2~~></td>
    <td><input type="checkbox" id="member[]" name="member[]" value="1" ~~CHECKED-MEMBER2~~></td>
</tr>

Php Code:

代码:

$page = $_POST['page'];
$admin = $_POST['admin'];
$staff = $_POST['staff'];
$member =$_POST['member'];

Now how to put default checkbox value 0 beform submitting the form in php

现在如何在 php 中提交表单之前将默认复选框值设置为 0

回答by RJParikh

Use isset()function if you need to set checkbox post value.

isset()如果您需要设置复选框发布值,请使用函数。

$page = isset($_POST['page']) ? $_POST['page'] : array();
$admin= isset($_POST['admin']) ? $_POST['admin'] : array();
$staff = isset($_POST['staff ']) ? $_POST['staff '] : array();
$member = isset($_POST['member']) ? $_POST['member'] : array();

If you need to perform checkbox checked then use checkedkeyword.

如果您需要执行复选框检查然后使用checked关键字。

<input type="checkbox" id="admin[]" name="admin[]" value="1" checked></td>

回答by RiggsFolly

If you want to set a checkbox as checkedfrom PHP or any code this is the way to do it.

如果你想checked从 PHP 或任何代码中设置一个复选框,这就是这样做的方法。

<input checked="checked" type="checkbox" id="admin[]" name="admin[]" value="1"></td>
       ^^^^^^^^^^^^^^^^^