1

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.

2
  • from where $selectedqualification came? you have $selected variable. do't you think? Commented Oct 6, 2017 at 6:20
  • sorry i have updated my question now Commented Oct 6, 2017 at 6:22

3 Answers 3

1

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']; ?>"
Sign up to request clarification or add additional context in comments.

Comments

1

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

use this :

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.