2

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?

8
  • Can you please clarify this: but they only will ever return "On" ? Commented Sep 27, 2015 at 1:17
  • Like I said in the question description, the $value variable stores "On" when a checkbox is checked, rather than the actual value string names (First, Second). Commented Sep 27, 2015 at 1:29
  • Also to clarify, the output is "on", all lowercase, not "On". My mistake. Commented Sep 27, 2015 at 1:32
  • I've tested the code and it works, it outputs First, Second. Make sure you add a submit button, otherwise you're not posting anything. Commented Sep 27, 2015 at 1:39
  • on is the value that is send for checked ckeckboxes that don’t have a value attribute set. Check your HTML code for errors – most likely you have written something wrong in there, so that the value attribute is not recognized by the parser. It is always a good idea to validate your HTML – validator.w3.org Commented Sep 27, 2015 at 1:40

2 Answers 2

2

When I tested your example on my server I receive the expected values. So I will provide you my code so you can edit it to suit your needs

<form action="" 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>
<input type='submit'>
</form>

<?php
if(!empty($_POST)){
    $box = '';
    foreach($_POST['samplecheck'] as &$value) {
        $box .= "Checked: $value \n";
    }
    echo $box;
}
?>

"I receive the expected values" which values did you receive ? – Pedro Lobito"

To answer this question.

If I do not select any checkbox, I receive no output.

If I select the "First" checkbox i receive the text

Checked: First

If I select the "Second" checkbox i receive the text

Checked: Second

If I select the "Second" checkbox and if I select the "First" checkbox I receive the text

Checked: First Checked: Second

Sign up to request clarification or add additional context in comments.

5 Comments

"I receive the expected values" which values did you receive ?
I used your php code but still received an echo output of "Checked: on Checked: on". Any idea as to what's going on here?
Unfortunately this can not be replicated based on the code you have provided. Please feel free to paste my code in at phpfiddle.org in the codespace section.
@Pedro Lobito That depends on which checkbox you check. If none, none, if the first one, First, if the second one Second, if both, both. IE: The expected values
@jesse , Ok, I've tested the code and I can also confirm that it works.
1

Credit to CBoe:

" on is the value that is send for checked checkboxes that don’t have a value attribute set. Check your HTML code for errors – most likely you have written something wrong in there, so that the value attribute is not recognized by the parser. It is always a good idea to validate your HTML – validator.w3.org "

In my case, and this is my fault for not including a more full code snippet, my checkboxes were sharing the same row as my radio, and simply placing them into separate rows allowed the checkbox value to function properly:

Bad code :

    <div class="row">
        <div class="large-6 medium-6 columns">
            <label>Choose</label>
            <input type="radio" name="rad_choices" value="Red" id="rad_id1"><label for="rad_id1">Radio 1</label>
            <input type="radio" name="rad_choices" value="Blue" id="rad_id2"><label for="rad_id2">Radio 2</label>
        </div>
        <div class="large-6 medium-6 columns">
            <label>Choose</label>
            <input name="samplecheck[]" type="checkbox" value="First" id="check_id1"><label for="check_id1">Checkbox 1</label>
            <input name="samplecheck[]" type="checkbox" value="Second" id="check_id2"><label for="check_id2">Checkbox 2</label>
        </div>
    </div>

Good code :

    <div class="row">
        <div class="large-6 medium-6 columns">
            <label>Choose</label>
            <input type="radio" name="rad_choices" value="Red" id="rad_id1"><label for="rad_id1">Radio 1</label>
            <input type="radio" name="rad_choices" value="Blue" id="rad_id2"><label for="rad_id2">Radio 2</label>
        </div>
    </div>

    <div class="row">
        <div class="large-6 medium-6 columns">
            <label>Choose</label>
            <input name="samplecheck[]" type="checkbox" value="First" id="check_id1"><label for="check_id1">Checkbox 1</label>
            <input name="samplecheck[]" type="checkbox" value="Second" id="check_id2"><label for="check_id2">Checkbox 2</label>
        </div>
    </div>

Why they can't be placed in the same row, I don't know. This might be due to another problem with my code that I'm failing to see, but this is an adequate solution for my purposes. For anyone with similar purposes, simply check your code for anything that would be affecting its ability to recognize or store the checkbox values.

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.