Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

PHP Checkbox Form Items

PHP Checkbox form items are returning as "on" in email. At first, I was having difficulty getting the check boxes to implode to the email sending function, but now they are not showing the value for the check boxes. So, if I select multiple check boxes, it will return an array of strings that are all set to "on".

How do I get the PHP process to capture the values associated with the checkbox that is selected?

<li class="servNI">
     <input type="checkbox" class="check" name="check[]" value"Large Screens">
     <label for="test">Large Screens</label>
</li>
<li class="servNI">
     <input type="checkbox" class="check" name="check[]" value"Lighting">
     <label for="test">Lighting</label>
</li>
$email_body = $email_body . "Interested in: ".implode(" ", $_POST['check']) . "<br /><br />";

2 Answers

Hi Joshua,

Checked checkboxes will have a value of "on" if a value attribute isn't specified in the html.

You don't have an equal sign on your value attributes so I suspect that is your problem.

Thank you!! Sometimes I make the dumbest mistakes (usually because I am in a hurry)! However, this is not fixing the problem in the php.

I know that I need to implode the value instead of the status, but I am uncertain about how to do this.

Here are two snippets that I found online, but they are not seeming to work correctly.

function IsChecked($checkArray,$value)
            {
                if(!empty($_POST[$chkname]))
                {
                    foreach($_POST[$chkname] as $chkval)
                    {
                        if($chkval == $value)
                        {
                            return true;
                        }
                    }
                }
                return false;
            }


-------------------------------------------------

        if (isset($_POST['Submit']))
            {
            if (isset($_POST['check']))
                {
                    foreach($_POST['check'] as $checkArray);
                }
            else
                {
                    $checkArray = "";
                }
            }

Have you verified that you're getting the expected values now from the individual checkboxes?

What are you currently getting for $email_body?

When I get the email, I get: "Interested in: on on on on on" ( I have about 20 checkboxes that I am doing this with. I put 2 in this post for simplicity.) Basically, I had selected 5 different services for testing this form.

Ok, so it looks like your implode is working properly but you still have the problem with "on"

You put equals in your value attribute like this: value="Large Screens"? And did you do it for all 20, save and refresh?

I hate to state the obvious but better to make sure.

I'm working on a couple of test files right now to test this locally.

Here's check_test.html:

<!doctype>
<html>
<head>
    <title></title>
</head>
<body>
    <form method="post" action="test.php">
        <input type="checkbox" name="check[]" value="check1">
        <input type="checkbox" name="check[]" value="check2">
        <input type="submit" value="submit">
    </form>
</body>
</html>

test.php

<?php 
 echo "Interested in: " . implode(" ", $_POST['check']) . "<br /><br />";
?>

Output is:

Interested in: check1 check2

If I remove the equal signs then I get:

Interested in: on on

I think this is an accurate representation of your code.

My best guess at this point is that you still have a problem in your html if you're getting "on" in your php.

Yes, that is the format that I have used for all of the list items in the checkbox series. I reloaded the page and tested it. I even changed the format of the spacing (in the email) from " " to " | " to make certain that the form wasn't pulling the process.php through the cache.

If process.php is the one containing the implode code then I don't think it would matter if that was cached or not because it wasn't changed.

If your html form is in a different file then it would matter if you were using a cached version of it.

Are you developing locally or on a remote server?

You could try the 2 files that I put up. At least they won't be cached and you'll know if the basic code is working.

Then we can try to spot what might be different with your code and my test setup.

My apologies, I missed those previous comments. Let me look at this really quick

Here is a larger sampling of what I have:

<li class="servNI">
    <input type="checkbox" class="cbox" name="check[]" value="MC">
    <label for="test">MC</label>
</li>
<li class="servNI">
    <input type="checkbox" class="cbox" name="check[]" value="Music Videos">
    <label for="test">Music Videos</label>
</li>
<li class="servNI">
    <input type="checkbox" class="cbox" name="check[]" value="Orchestra (live)">
    <label for="test">Orchestra (live)</label>
</li>
<li class="servNI">
    <input type="checkbox" class="cbox" name="check[]" value="Photo Booths">
    <label for="test">Photo Booths</label>
</li>
<li class="servNI">
    <input type="checkbox" class="cbox" name="check[]" value="Projectors">
    <label for="test">Projectors</label>
</li>
<li class="servNI">
    <input type="checkbox" class="cbox" name="check[]" value="Staging">
    <label for="test">Staging</label>
</li>
<li class="servNI">
    <input type="checkbox" class="cbox" name="check[]" value="Video Dance Party">
    <label for="test">Video Dance Party</label>
</li>

I am uploading to a temporary website through a free hosting service. Every change that I am making is being uploaded through an FTP

The cache was the problem! That completely fixed it! Thank you so much!!!!

Try clearing your cache then in case the form is cached.

Did you try yet to upload my 2 test files? Since these are new files it will eliminate any caching problems.

The "on" value is an html issue and is something that is created by the browser as it's getting the form ready for submission so I don't think that this can be a php problem. The browser is sending the php script that "on" value.

Success! I'm glad you got it working because I was out of ideas. :)

Thank you for keeping it all in the comments too.

Not a problem! Thanks again Jason!

Jacob Herper
Jacob Herper
94,150 Points

I would simply give them unique names, at least that way they will return the set values.