如何阅读,如果一个复选框被选中在PHP?


当前回答

根据表单的操作,您可以在$_POST或$_GET数组中检查相应的值是否已设置且非空。

例如:使用一个名为"test"的POST表单(即:<input type="checkbox" name="test">,你会使用:

if(isset($_POST['test']) {
   // The checkbox was enabled...

}

其他回答

filter_input(INPUT_POST, 'checkbox_name', FILTER_DEFAULT, FILTER_FORCE_ARRAY)

Wordpress有checked()函数。 参考:https://developer.wordpress.org/reference/functions/checked/

checked( mixed $checked, mixed $current = true, bool $echo = true )

描述 比较前两个参数,如果相同则标记为选中

参数 美元的检查 (混合)(必选)要比较的值之一

当前美元 (混合)(可选)(true)如果不是true,要比较的另一个值 缺省值:true

美元的回声 (可选)是否返回或返回该字符串 缺省值:true

返回#返回 (string) HTML属性或空字符串

了解isset,它是一个内置的“函数”,可以在if语句中使用,以判断一个变量是否已被使用或设置

例子:

    if(isset($_POST["testvariabel"]))
     {
       echo "testvariabel has been set!";
     }

我已经把它固定成一个带有复选框的PHP表单

$categories = get_terms(['taxonomy' => 'product_cat', 'hide_empty' => false]); Foreach ($categories为$ categororie) { Echo "<input type="checkbox" value="$ categororie ->term_taxonomy_id" name="catselected[]"> $ categororie ->slug"; }

这样我把它添加到Woocommerce表。 wp_set_post_terms($product_id, $_POST['catselected'], 'product_cat');

<form>
<input type="check" id=chk1 value="1">
<input type="check" id=chk2 value="1">
<input type="check" id=chk3 value="1">
</form>

当你检查chk2时,你可以看到如下值:

<?php
foreach($_POST as $key=>$value)
{
    if(isset($key))
        $$key=strip_tags($value);
}
insert into table (chk1,chk2,chk3) values ('','1','');
?>