samedi 26 septembre 2015

HTML/PHP Checkbox Values Only Return "On"

Brand new to both HTML and PHP so forgive me for what is probably an elementary question.

This code is for a skeletal HTML/PHP form that emails some user input. I've been searching for many hours on a fix for this, but I haven't been able to get a solution.

I'm trying to find a way to store the actual strings of the checkbox values (i.e. First, Second) instead of having the php code return "On" boolean values for either checkbox when they're checked.

Here's the relevant code below:

Form:

<form action="formhandle.php" method="post">

<div class="row">
    <label>Choose</label>
    <input name="samplecheck[]" type="checkbox" value="First"><label>Checkbox 1</label>
    <input name="samplecheck[]" type="checkbox" value="Second"><label>Checkbox 2</label>
</div>

</form>

Handler method 1:

$value = $_POST['samplecheck'];

for($i=0; $i < count($value); $i++){
    $box .= "Checked : " . $value[$i] . "\n";
}

Handler method 2:

foreach($_POST['samplecheck'] as $value) {
    $box .= "Checked: $value \n";
}

I know there's limited validation but that's not my concern right now. Both handling methods "work" in that they properly recognize when a checkbox value is "On" or "Off", but they only will ever return "On", and I have no idea how to return the actual string of the value.

In either case, if both checkboxes are checked, I'll get this output for $box :

Checked : On Checked : On

What am I doing wrong? Is it only ever supposed to return On? If so, how am I supposed to tell each array entry apart?




Aucun commentaire:

Enregistrer un commentaire