Shreyas Achar
Shreyas Achar

Reputation: 1435

Binded values of checkbox is not storing into array

This post is regarding my previous question continuation Bind values to checkbox

These Below code i have used in one of my php page

$Qualification=$_SESSION['Qualification']; // gives value = 1,2,3,
        $selectedqualification= explode(",", $Qualification);
        $selectedqualification= array_filter($selected);

Now these values are used to bind into checkbox-

<?php  $query = "SELECT * FROM Qualification";

  $result = mysqli_query($dbcon,$query);
  while($row=mysqli_fetch_array($result)){ 
?>  
<input type='checkbox' value='"<?php $row['Id'] ?> "' name='Qualification[]'  <?php if (in_array($row['Id'], $selectedqualification)){ echo "checked"; } ?> /><?php echo $row['Description'] ?><br>

<?php } ?>

The Output of these will be displayed as

[/]Teacher
[/]ENgineer
[/]Doctor
[]Banker

Where the three values are checked .

Now on submit of the button using method post in the form

if(isset($_POST['submit']))
{

   $AllQualification="";

print_r($_POST['Qualification']);

        foreach($_POST['Qualification'] as $Qualification) 
        {
           $AllQualification .=$Qualification.",";
        }
echo $AllQualification

}

The output will be

Array ( [0] => " " [1] => " " [2] => " " [3] => " ")
'" "," "," "

Values are not getting passed to the array. How to get the values to bind to array? Any help appreciated.

Upvotes: 1

Views: 51

Answers (3)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

You have to change check-box html like below:-

<input type='checkbox' value="<?php echo $row['Id']; ?>" name='Qualification[]'  <?php if (in_array($row['Id'], $selectedqualification)){ echo "checked"; } ?> /><?php echo $row['Description'] ?><br>

CHANGE I MADE (added echo and removed single quotes):-

value="<?php echo $row['Id']; ?>"

Upvotes: 1

Kinshuk Lahiri
Kinshuk Lahiri

Reputation: 1500

There are lot of issue in the code.

1) No echo and use wrong quotes for below, fixed:

<input type="checkbox" value="<?php $row['Id'] ?>" name="Qualification[]"  <?php if (in_array($row['Id'], $selectedqualification)){ echo "checked"; } ?> /><?php echo $row['Description'] ?><br>

2) Use implode, rather than concatenating it.

$qualification = implode(",",$_POST['Qualification']);

PS: Don't forget to add a check if the array is empty or not.

Hope this helps.

Upvotes: 0

usman ikram
usman ikram

Reputation: 445

Man you are missing echo in checkbox value and also space is there:

use this :

value='"<?php echo $row['Id'] ?>"'

Upvotes: 1

Related Questions