如何阅读,如果一个复选框被选中在PHP?
当前回答
$is_checked = isset($_POST['your_checkbox_name']) &&
$_POST['your_checkbox_name'] == 'on';
短路计算将会注意,这样您就不会在your_checkbox_name未提交时访问它。
其他回答
如果你的HTML页面是这样的:
<input type="checkbox" name="test" value="value1">
提交表单后,您可以检查它:
isset($_POST['test'])
or
if ($_POST['test'] == 'value1') ...
<?php
if(isset($_POST['nameCheckbox'])){
$_SESSION['fr_nameCheckbox'] = true;
}
?>
<input type="checkbox" name="nameCheckbox"
<?php
if(isset($_SESSION['fr_nameCheckbox'])){
echo 'checked';
unset($_SESSION['fr_nameCheckbox']);
}
?>
filter_input(INPUT_POST, 'checkbox_name', FILTER_DEFAULT, FILTER_FORCE_ARRAY)
我已经把它固定成一个带有复选框的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');
<?php
if (isset($_POST['add'])) {
$nama = $_POST['name'];
$subscribe = isset($_POST['subscribe']) ? $_POST['subscribe'] : "Not Checked";
echo "Name: {$nama} <br />";
echo "Subscribe: {$subscribe}";
echo "<hr />";
}
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<input type="text" name="name" /> <br />
<input type="checkbox" name="subscribe" value="news" /> News <br />
<input type="submit" name="add" value="Save" />
</form>