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


当前回答

如果你的HTML页面是这样的:

<input type="checkbox" name="test" value="value1">

提交表单后,您可以检查它:

isset($_POST['test'])

or

if ($_POST['test'] == 'value1') ...

其他回答

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

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

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

}

我已经把它固定成一个带有复选框的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');

在BS3中

  <?php
                  $checked="hola";
                  $exenta = $datosOrdenCompra[0]['exenta'];
                  var_dump($datosOrdenCompra[0]['exenta']);
                  if(isset($datosOrdenCompra[0]['exenta']) and $datosOrdenCompra[0]['exenta'] == 1){

                      $checked="on";

                  }else{
                    $checked="off";
                  }

              ?>
              <input type="checkbox" id="exenta" name="exenta" <?php echo $checked;?> > <span class="label-text"> Exenta</span>

请注意isset($datosOrdenCompra[0]['exenta'])的用法

<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','');
?>

当使用复选框作为数组时:

<input type="checkbox" name="food[]" value="Orange">
<input type="checkbox" name="food[]" value="Apple">

你应该使用in_array():

if(in_array('Orange', $_POST['food'])){
  echo 'Orange was checked!';
}

记得先检查数组是否被设置,例如:

if(isset($_POST['food']) && in_array(...