简体   繁体   English

如何使复选框被选中

[英]How to Make Check box is checked

如何使用 PHP 选中我的复选框,当我稍后访问该页面时,我希望选中以前选择的复选框

<input  name="product[]" type="checkbox" value="1" />

Start Session as开始会话为

    <?php
session_start();
$session_products = array();
if(array_key_exists("products", $_SESSION)) 
{
  if($_SESSION["products"] != null)
  {
    $session_products = $_SESSION["products"];
  }
}

?>

Change your code as follows更改您的代码如下

<input  name="product[]"  type="checkbox" value="1" <?php if(in_array("1", $session_products)) echo "checked='checked'"; ?>/>

You can add a checked .您可以添加一个checked .

Here an example:这里有一个例子:

<input  name="product[]"  type="checkbox" value="1" checked />

By the way, your value=1 seems to be wrong.顺便说一句,您的value=1似乎是错误的。 Normally you use `value to distinguish items.通常,您使用 `value 来区分项目。

Example:例子:

<input  name="product[]"  type="checkbox" value="dvd" checked />
<input  name="product[]"  type="checkbox" value="cd" />

to do something like this with phpphp做这样的事情

<input  name="product[]"  type="checkbox" value="1" <?php echo ($value =="1") ? "checked" : ""; ?> />

where $value is the value from database其中$value是来自数据库的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM