lundi 28 décembre 2015

Php form checked when the checkbox value in array mysql query?

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.




Aucun commentaire:

Enregistrer un commentaire