Question

I have a list of candidates for the position of Business Managers. The voter needs to select at least two candidates. I used check boxes in order that the voter can do multiple select. And when the voter clicks on next button to proceed voting to other positions , the two selected Business Manager should be carried..The problem is I can get the two but with the same Name.

For example:Voter selected

        John Smith and Harry Potter

The result when i get the value is only the last person selected. I noticed in my check inbox I used only one name="choice" so it is not possible to get two....I cann assign different name for each check inbox when my list is static but in my case i used the dyanmic way to place them in array of check boxex which means they have the same name="choice"...I hope you understand my point...Please help me..Below is my code implementation..

  1. Result of populating the list of check boxes:

        $result = mysql_query($QUERY);
                        while($row = mysql_fetch_object($result)){
                            $Name = $row->Name;
                            $PLName = $row->PLName;
                            $CID = $row->CID;
                            echo '<tr><td>';
                            echo '<img src="fetchImage.php?CID=' . $CID . ' " alt="Candidate photo" width="150px" height="135px" style="border:2px solid silver;">';
                            echo '</td>';
                            echo '<td><div class="contact_info_title">' . $Name . '</div><br>';
                            echo  $PLName . '<br><br>';
                            if(isset($_SESSION['VLPCCBusManager'])){
                                if($_SESSION['VLPCCBusManager'] == $CID){
                                    echo '<input type="checkbox" name="Choice" value=' . $CID . ' Checked>Select </td></tr>';
                                }else{
                                    echo '<input type="checkbox" name="Choice" value=' . $CID . '>Select </td></tr>';
                                }
                            }else{
                                echo '<input type="checkbox" name="Choice" value=' . $CID . '>Select </td></tr>';
                            }
                        }
                    echo '';
    
  2. Getting the selected choices when submit is clicked moving to another form:

    if(isset($_POST['submit'])){
    if(isset($_POST['Choice'])){
        if(empty($_POST['Choice'])){ $errors='Choice'; }
        if(empty($errors)){ 
            $Choice = $_POST['Choice'];
            $_SESSION['VLPCCBusManager'] = $Choice;
            $_SESSION['VLPCCBusManager2'] = $Choice;
            header("Location: VLPCCSgtArms.php?ChoiceCat=$ChoiceCat");
        }
    }else{
        $_SESSION['VLPCCBusManager'] = '0';
        $_SESSION['VLPCCBusManager2'] = '0';
        header("Location: VLPCCSgtArms.php?ChoiceCat=$ChoiceCat");
    }
    

    }

Was it helpful?

Solution 4

To get more than one value from checkbox

  1. You need to change the name of checkboxes to an array like this
<input type="checkbox" name="Choice[]" />
  1. while reading the post value of check box read like this

$_POST['Choice'][0],$_POST['Choice'][1],...

Example

 <input type="checkbox" name="Choice[]" value=" Checkbox 1"/> Checkbox 1<br>
 <input type="checkbox" name="Choice[]" value=" Checkbox 2"/> Checkbox 2<br>
 <input type="checkbox" name="Choice[]" value=" Checkbox 3"/> Checkbox 3<br>

If you had checked only checkbox 1 and 3 , then you can get values like this

$_POST['Choice'][0] => has the value of first checkbox that has been checked.

so $_POST['Choice'][0] will have value of Checkbox 1

$_POST['Choice'][1] => has the value of second checkbox that has been checked.

so $_POST['Choice'][1] will have value of Checkbox 3

OTHER TIPS

You will need to specify the name of checkboxes as:

<input type="checkbox" name="choice[]" />

and not not simply

<input type="checkbox" name="choice" />.

Now, for example, you have this in HTML:

<input type="checkbox" name="choice[]" value="choice1"> Choice1
<input type="checkbox" name="choice[]" value="choice2"> Choice2
<input type="checkbox" name="choice[]" value="choice3"> Choice3

Let's say, you tick the first and third checkbox,

In PHP, you can check the checkboxes checked as:

<?php

print_r($_POST['choice']);

?>

The output will be:

Array('0' => 'choice1',
      '1' => 'choice3')

I hope this explains your doubt.

You will need to specify the name of checkboxes as an array:

then you get the values in array and then you can save in the database

you can mention the check boxes names differently such as 1,2,3,4,5,6.. and you post values using the form and get it as an array for example

$values[0]=$_POST['1'];
    $values[1]=$_POST['2'];
    $values[2]=$_POST['3'];
    $values[3]=$_POST['4'];
    $values[4]=$_POST['5'];
    $values[5]=$_POST['6'];
    $values[6]=$_POST['7'];
    $values[7]=$_POST['8'];
    $values[8]=$_POST['9'];
    $values[9]=$_POST['10'];

    and then u can implode it to a variable and save it in the database 

    $skill_set = implode(",", $values);

You have to change your code to

name="choice[]"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top