Jump to content

Why are checkboxes such a pain?


bluegray

Recommended Posts

Hi Php Freaks!

 

I'm trying to submit checkboxes through a form, but it doesn't seem to be working like "text" type inputs.

 

Here's the Xhtml:

<label for="veggies">Vegetables
     <input type="checkbox" name="testbox [veg]" id="veggies" value="veggies" />
</label>

 

Here's the Php:

$testbox = $_POST ['testbox'] ;
var_dump ($testbox) ;

 

Here are my results:

Notice: Undefined index: testbox in C:\Program Files\WAMP\www\test\form.php on line 72

 

NULL

 

And here is me getting rocked by check boxes:

:'(

 

Anywhere I can go to be explained the nuances of checkboxes?

Link to comment
Share on other sites

The name you gave your's contains a space and it is an array. It produces the following (php converts the space in the name to an under-score in order to make it a valid variable name) -

 

    [testbox_] => Array
        (
            [veg] => veggies
        )

 

Link to comment
Share on other sites

The name you gave your's contains a space and it is an array. It produces the following (php converts the space in the name to an under-score in order to make it a valid variable name) -

 

    [testbox_] => Array
        (
            [veg] => veggies
        )

 

I'm not sure I get what you mean...I don't see a space in the name of my array.  Why would php randomly insert an underscore?

Link to comment
Share on other sites

name="testbox [veg]"

 

Notice the space before [veg].  That is why the testbox has a space in it.

 

name="testbox[veg]"

 

Will solve that issue.

 

Ah, so whenever referring to an array, am I supposed to leave no spaces between the array name and the index/key box?

 

Edit: Rats!  I removed the the space, but am still getting the undefined index notice once I submit the form.  =(

 

Just as an experiment, I've switched the input "type" to text, and the problem went away.  So it's definitely an issue with the kind of input.  Does anyone know how to circumvent this problem?  Other than avoiding checkboxes altogether.

Link to comment
Share on other sites

You do not need to do spaces after, IE:

 

$array ['index']

 

 

Is generally looked down upon.

$array['index']

 

Is the "preferred" way by most coders. Not sure where you got the idea to space between them. But show the full relevant code, specifically where the notice error is thrown, so we can help you.

Link to comment
Share on other sites

You do not need to do spaces after, IE:

 

$array ['index']

 

 

Is generally looked down upon.

$array['index']

 

Is the "preferred" way by most coders. Not sure where you got the idea to space between them. But show the full relevant code, specifically where the notice error is thrown, so we can help you.

 

 

Here's the portion of the form that's bringing up the problem:

<!-- Check Boxes! -->
			<div class="form_sect">
				<span class="form_cat">Favorite Foods</span>
				<label for="fruits">Fruits
					<input type="checkbox" name="testbox[fruits]" id="fruits" value="fruits" />
				</label>

				<label for="salad">Salad
					<input type="checkbox" name="testbox[salad]" id="salad" value="salad" />
				</label>

				<label for="veggies">Vegetables
					<input type="checkbox" name="testbox[veg]" id="veggies" value="veggies" />
				</label>

				<label for="soup">Soup
					<input type="checkbox" name="testbox[soup]" id="soup" value="soup" />
				</label>
			</div>

 

Here's the line that's being pointed to in the notice:

$testbox = $_POST['testbox'] ;

 

And that's really all that's relevant as far as I can tell.

 

This is just some added context:

 

"Just as an experiment, I've switched the input "type" to text, and the problem went away.  So it's definitely an issue with the kind of input.  Does anyone know how to circumvent this problem?  Other than avoiding checkboxes altogether."

 

Link to comment
Share on other sites

Well with checkboxes, if the item is not checked it is not going to be in the POST data. Is one of the checkboxes checked?

 

To avoid the notice error you can do this:

$testbox = isset($_POST['testbox'])?$_POST['testbox']:null;

 

Which will set textbox to null if no items were checked.

Link to comment
Share on other sites

Well with checkboxes, if the item is not checked it is not going to be in the POST data. Is one of the checkboxes checked?

 

To avoid the notice error you can do this:

$testbox = isset($_POST['testbox'])?$_POST['testbox']:null;

 

Which will set textbox to null if no items were checked.

 

Can you explain the "?" and the ":" please?  I'm not familiar with those.

Link to comment
Share on other sites

Ternary operator, it acts a shortened if else.

 

Basically it goes 

if (isset($_POST['testbox'])) {
    $testbox = $_POST['testbox'];
}else {
    $testbox = null;
}

 

Same thing, just shortened.

Link to comment
Share on other sites

Ternary operator, it acts a shortened if else.

 

Basically it goes 

if (isset($_POST['testbox'])) {
    $testbox = $_POST['testbox'];
}else {
    $testbox = null;
}

 

Same thing, just shortened.

 

Sweet stuff.  Okay... I used the shorter version because as you said, it's neater.  I'm still having some issues with the nature of checks though.

 

How would you handle processing them into an SQL database when they're part of a larger form? 

 

The problem is that when, say 5 out of 10 boxes are checked, the array that's filled only has 5 elements; whereas if it was a text type input the array would have 10 elements with 5 nulls.

 

Right now, it seems the best plan is to sort them into a separate array from text type inputs, with an argument representing the number of values that Could be received, and have a custom function fill all the empties with a null. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.