Skip to content Skip to sidebar Skip to footer

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. Normal

Solution 1:

=>Try this Code I hope it's useful.

// 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) {
    ?><inputtype='checkbox'name='state[]'style="margin-left:5px;"value="<?phpecho$state_list['id']; ?>"checked  ><?phpecho$state_list['name']; ?><br><?php$flag=false;
  } else { 
    ?><inputtype='checkbox'name='state[]'style="margin-left:5px;"value="<?phpecho$state_list['id']; ?>"     ><?phpecho$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++;
}
?>

Solution 2:

Don't forget to sanitize $_POST, it is good coding practice to not access the POST superglobal directly.

+1 for initializing your variables before using them :)

Post a Comment for "Php Form Checked When The Checkbox Value In Array Mysql Query?"