Jump to content

Foreach w/ if and else on Checkboxes - Cannot get this working without errors


OldWest

Recommended Posts

Excuse my late night slop!

 

But I read a thread earlier and I am working on a dynamic way to address checkboxes with some intuitive messages while processing, and I cannot get this to work right w/out errors..

 

Any ideas where I am going totally wrong with this?

 

<form name="" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
One <input type="checkbox" name="checkbox[]" value="checkOneValue" /><br />
Two <input type="checkbox" name="checkbox[]" value="checkTwoValue" /><br />
Three <input type="checkbox" name="checkbox[]" value="checkThreeValue" /><br />
Four <input type="checkbox" name="checkbox[]" value="checkFourValue" /><br />
Five <input type="checkbox" name="checkbox[]" value="checkFiveValue" /><br />
<input type="submit" name="submit" value="Echo Checkbox Data!" />
</form>

<?php

if (isset($_POST['submit'])) {

$checkbox = $_POST['checkbox'];

foreach ($checkbox as $checkboxValue)

		if (empty($checkboxValue)) {

			echo "$checkboxValue is emtpy";

	} else {
		    
			echo "<br>$checkboxValue is checked";
	}

};
?>

 

When no checkboxes are selected, I am dealing with this S%#$! Otherwise it works as intended..

 

Notice: Undefined index: checkbox in C:\wamp\www\php_practice\0\checkbox_echoing_1.php on line 24

 

Warning: Invalid argument supplied for foreach() in C:\wamp\www\php_practice\0\checkbox_echoing_1.php on line 26

 

 

Link to comment
Share on other sites

The variable ($checkbox) used in your foreach isn't defined anywhere is the first thing I see.

 

I realized after I posted and then modified my post code to have

$checkbox = $_POST['checkbox'];

BEFORE the foreach..

 

When I select boxes and submit all works as intended, but when nothing is selected I get this junk:

 

Notice: Undefined index: checkbox in C:\wamp\www\php_practice\0\checkbox_echoing_1.php on line 24

 

Warning: Invalid argument supplied for foreach() in C:\wamp\www\php_practice\0\checkbox_echoing_1.php on line 26

Link to comment
Share on other sites

thorpe,

that def removed the errors, but i cannot get it to work as intended now.. throwing in the towel for tonight.. I just don't understand while I keep getting a parse error on this. I tried about 5 variations of the else and braces etc and it won't give - painful at this point! Maybe I need to run the foreach twice for each if and else? Tried that but did not work.. The parse error I am getting is on the

else 

line.

 

Parse error: parse error in C:\wamp\www\php_practice\0\checkbox_echoing_1.php on line 30

 

<?php

if (isset($_POST['checkbox'])) { 

	$checkbox = $_POST['checkbox'];

		foreach ($checkbox as $checkboxValue) { 

			echo "<br>$checkboxValue is checked";

		else  { 

			echo "$checkboxValue is not checked."; } 

			} 

		};
?>

 

Link to comment
Share on other sites

You need an if() in there somewhere, or lose the else{}.

 

foreach() {
} else {
}

 

Interesting nesting with foreach... Well I am not sure if this code is very efficient, but I segmented it out and added some "thought" to the process but the below works..

 

I think my next step is to try to figure out how to

echo 

the values that were NOT selected, but not sure if that's possible without javascript seeing that they must be set in order to get the value (at least I think!).

 

Am I correct in my understanding? For example, if I wanted to echo to the browser:

 

$valueOne was checked.

$valueTwo was not checked.

$valueThreewas not checked.

....

 

<?php

if (isset($_POST['checkbox']) && (isset($_POST['submit']))) { 

$checkbox = $_POST['checkbox'];

	foreach ($checkbox as $checkboxValue) { 

			if (!empty($checkboxValue)) {
					echo "<br />$checkboxValue is checked";
				}
			}	
		}; // End of foreach loop

		if (!isset($_POST['checkbox']) && (isset($_POST['submit']))) {
					echo "<br />No checkboxes were selected!";
};

?>

 

Link to comment
Share on other sites

Also for any nooby who might be following this thread, you can maintain the checkbox checkmark by doing this:

 

<?php if(isset($_POST['checkbox'])) echo "checked"; ?>

 

For example:

 

<input type="checkbox" name="checkbox[]" value="checkOneValue" <?php if(isset($_POST['checkbox'])) echo "checked"; ?> />

 

So basically once the form is submitted, the checkbox will stay in the box so as to inform the user what he/she selected - I guess its user friendly  :shrug:

Link to comment
Share on other sites

i suggest that you use checked='checked' for xhtml compliance. i also prefer curly brackets on all if statements (i avoid "sometimes" whenever possible). but that's me.

 

<input type="checkbox" name="checkbox[]" value="checkOneValue" <?php if(isset($_POST['checkbox'])) { echo "checked='checked'"; } ?> />

 

 

Link to comment
Share on other sites

To display the checked AND unchecked status, if you have the array of values of the checkboxes that were used on the form, you can loop through them and compare using in_array(). This works, and should give you a start on it, or at least some ideas.

 

<?php
$form_checkbox_values = array( 'val1', 'val2', 'val3', 'val4', 'val5' );
if( is_array($_POST['checkbox']) ) {
foreach( $form_checkbox_values as $v ) {
	if (in_array( $v, $_POST['checkbox']) ) {
		echo "$v is checked.<br>\n";
	} else {
		echo "$v is not checked<br>\n";
	}
}
}
?>
<form method="post" action="">
1 <input type="checkbox" name="checkbox[]" value="val1"><br>
2 <input type="checkbox" name="checkbox[]" value="val2"><br>
3 <input type="checkbox" name="checkbox[]" value="val3"><br>
4 <input type="checkbox" name="checkbox[]" value="val4"><br>
5 <input type="checkbox" name="checkbox[]" value="val5"><br>
<input type="submit" name="submit" value="Submit">
</form>

Link to comment
Share on other sites

Nice

 in_array

might be exactly what I needed.. I figure 1,000,000 programmers before me ran into this same issue, so php seems to be way ahead of me! Thanks for the guidance. I'm going to reformulate my code to get this ticking..

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.