0

I want to create a list of checkboxes with values from a php array as its label. I want it to look like

Here is the list of students whose schedules are saved:

[checkbox] Robb

[checkbox] Catelyn

[checkbox] Lady Stoneheart

but my code does not work.

Here's my code:

<?php

    $students = $_SESSION['students'];

    echo "Here is the list of students whose schedules are saved:<br/><br/>";

    echo "<form action='checkbox-form.php' method='post'>
    Load student?<br/>";

    foreach ($students as $stud) {

        echo "<br/><input type='checkbox' name=" . $stud . " value='load' />";
    }

    echo "<br/><br/><input type='submit' name='formSubmit' value='Submit' />
    </form>";

?>

The array is not the problem because it contains the proper values when I print it through foreach.

3
  • 1
    What do you mean by 'it doesn't work'? It doesn't do anything? Produces the wrong data? You're not outputting anything other than the HTML for the checkbox - there's no label being echoed. Commented Jun 5, 2013 at 17:53
  • 1
    Oh snap. Closing this question for total self-fail. Commented Jun 5, 2013 at 17:55
  • 1
    Nothing another coffee wouldn't fix. Commented Jun 5, 2013 at 17:56

4 Answers 4

1

It might be easier to do it this way:

On the form:

foreach ($students as $stud) {

    echo "<br/><input type='checkbox' name=\"students[]\" value='$stud' />$stud<br>";
}

On the handler to see what it's passing:

print_r($_POST);
Sign up to request clarification or add additional context in comments.

3 Comments

Wait... gotta escape my slashes. There, that should do it.
Neat. I changed the double quotes in students[] to single ones and it worked. I didn't know about the escape slashes, heh. Any tips on how to align the checkboxes? It looks like a zigzagged list because the CSS of the page I'm displaying it to is aligned center, and some of the names vary in length so it looks weird on a center align. Am I making sense?
You can wrap them in a <div> or a <p> and assign a width and make them float. You can also make them an ordered list <ol> and apply styling to <li> tags with floats.
1

If all of the "value" fields are "load", which in this case they are, nothing can happen because your PHP won't see anything different value-wise. You should set the name value of all of these checkboxes to the same thing, and set the value to the student's name (though that's bad design- you should be setting the value to the numeric DB id that represents the student- what if you have students with the same name?)

So:

for($i = 0; i < count($students); $i++) {
    $s = $students[$i];
    echo "<br/><input type='checkbox' name="students[]" value='$s' />";
}

In this case name="students[]" is the students ARRAY which you can access via $_POST['students'] as an array.

Comments

1

It looks like you've confused the "name" attribute for the label. A few notes:

  • "name" is used as the name of the parameter passed to the backend
  • "value" is the value that will be assigned to that parameter if the checkbox is checked

So the line in your foreach should look more like:

echo '<br /><input type="checkbox" name="students[]" value="'.$stud.'" />'.$stud;

If Robb and Catelyn are checked you will get the below in the $_POST['students'] variable server side:

Array
(
  [0] => Robb
  [1] => Catelyn
)

Comments

1
foreach($students as $student){
echo "<br/><input type='checkbox' name=" . $student . " value=" . $student . " />";
echo "<label for name=" . $student . ">" . $student . "</label>";   
}   

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.