php表单检查当数组mysql中的checkbox值是否查询? [英] Php form checked when the checkbox value in array mysql query?

查看:87
本文介绍了php表单检查当数组mysql中的checkbox值是否查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的价值在我的数据库。它的调用类型



我的html表单中有两个复选框。

 < input name =barea []type =checkboxid =barea []value =1{$ cbvar} /& b $ b< input name =barea []type =checkboxvalue =2id =barea []{$ cbvar} /> Gold 
/ pre>

我将数据存储为以下代码:

  $ checkboxvar = implode(',',$ _GET ['barea']); 

所以我的表数据如下:

  + ------- + ----------- + 
| ID |类型|
+ ------- + ----------- +
| 1 | 1 |
| 2 | 1,2 |
| 3 | 2 |
| 4 | 1,2 |
| 5 | 1,2 |
| 6 | 1 |
+ ------------------- +


$ b b

当用户编辑此数据时,如何 barea []值存在时检查 mysql_query



我试试下面的编码的c $ c> in_array 在我的php文件中:

  $ checkbox = explode(',',$ row ['type']); 
if(in_array($ _ GET ['barea'],$ checkbox)){
$ cbvar =checked = \checked\;
} else {
$ cbvar ='';
}

在我的html

 < input name =barea []type =checkboxid =barea []value =1{$ cbvar} /& < input name =barea []type =checkboxvalue =2id =barea []{$ cbvar} /> Gold 

但它不能工作,我想也许问题是 if(in_array($ _ GET ['barea'],$ checkbox) {



如何改善我的编码,或任何其他好的编码建议?谢谢。

解决方案

// html page ..

 < label>选择州< / label>< br> 
<?php
$ allgroup = mysql_query(SELECT * FROM state);
$ flag = false;
while($ state_list = mysql_fetch_array($ allgroup))
{
$ parr = explode(',',$ er ['state_id']);
$ size = sizeof($ parr);
for($ i = 0; $ i< $ size; $ i ++){
if($ parr [$ i] == $ state_list ['id']){
$ flag = true;
}
}

if($ flag == true){

< input type ='checkbox'name ='state []'style =margin-left:5px; value =<?php echo $ state_list ['id'];?>检查> <?php echo $ state_list ['name']; > < br>
<?php
$ flag = false;
} else {
?>
< input type ='checkbox'name ='state []'style =margin-left:5px; value =<?php echo $ state_list ['id'];?> > <?php echo $ state_list ['name']; > < br>
<?php
}
}
?>

// php代码。

 <?php 
$ states =;
$ i = 0;
foreach($ _POST ['state'] as $ selected){
echo sizeof($ _ POST ['state']);
if($ i == sizeof($ _ POST ['state']) - 1){
$ states = $ states。
} else {
$ states = $ states。$ selected。,;
}
$ i ++;
}
?>


I have and value in my database. It's call type

I have 2 checkbox in my html form.

<input name="barea[]" type="checkbox" id="barea[]" value="1" {$cbvar}/>Normal 
<input name="barea[]" type="checkbox" value="2" id="barea[]" {$cbvar}/>Gold

And I store data as code below:

$checkboxvar = implode(',', $_GET['barea']);

So my table data like:

+-------+-----------+
| ID    | type      |
+-------+-----------+
| 1     | 1         |
| 2     | 1,2       |
| 3     | 2         |
| 4     | 1,2       |
| 5     | 1,2       |
| 6     | 1         |
+-------------------+

When user edit this data, how to checked the checkbox when the barea[] value exist in_array in the mysql_query?

I try the below coding, in my php file:

$checkbox = explode(',',$row['type']);
if (in_array($_GET['barea'],$checkbox)){
    $cbvar = "checked=\"checked\"";
}else{
    $cbvar = '';
}

In my html

<input name="barea[]" type="checkbox" id="barea[]" value="1" {$cbvar}/>Normal 
<input name="barea[]" type="checkbox" value="2" id="barea[]" {$cbvar}/>Gold

But it can't work, i think maybe the problem is on if (in_array($_GET['barea'],$checkbox)){.

so how to improve my coding, or any other good coding suggestion? thank you.

解决方案

// html page..

<label>Select State</label><br>
<?php 
$allgroup = mysql_query("SELECT * FROM  state");
$flag=false;
while($state_list = mysql_fetch_array($allgroup))
{
   $parr=explode(',',$er['state_id']);
   $size = sizeof($parr);
   for($i=0;$i<$size;$i++) { 
     if($parr[$i]==$state_list['id']) {                              
        $flag=true;
     } 
   }    

  if($flag==true) {
    ?>
    <input  type='checkbox' name='state[]' style="margin-left:5px;" value="<?php echo $state_list['id']; ?>" checked  > <?php echo $state_list['name']; ?> <br>
    <?php
    $flag=false;
  } else { 
    ?>
    <input  type='checkbox' name='state[]' style="margin-left:5px;" value="<?php echo $state_list['id']; ?>"     > <?php echo $state_list['name']; ?> <br>
    <?php
    }
}
?>

// php code .

<?php
$states="";
$i=0;
foreach( $_POST['state'] as $selected) {
   echo sizeof($_POST['state']);
   if($i==sizeof($_POST['state'])-1) {
       $states = $states.$selected;
   } else {
     $states = $states.$selected.",";
   }
   $i++;
}
?>

这篇关于php表单检查当数组mysql中的checkbox值是否查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆