OldWest Posted October 26, 2010 Share Posted October 26, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/216855-foreach-w-if-and-else-on-checkboxes-cannot-get-this-working-without-errors/ Share on other sites More sharing options...
trq Posted October 26, 2010 Share Posted October 26, 2010 The variable ($checkbox) used in your foreach isn't defined anywhere is the first thing I see. Quote Link to comment https://forums.phpfreaks.com/topic/216855-foreach-w-if-and-else-on-checkboxes-cannot-get-this-working-without-errors/#findComment-1126544 Share on other sites More sharing options...
Pikachu2000 Posted October 26, 2010 Share Posted October 26, 2010 What are the errors you're getting? Quote Link to comment https://forums.phpfreaks.com/topic/216855-foreach-w-if-and-else-on-checkboxes-cannot-get-this-working-without-errors/#findComment-1126545 Share on other sites More sharing options...
OldWest Posted October 26, 2010 Author Share Posted October 26, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/216855-foreach-w-if-and-else-on-checkboxes-cannot-get-this-working-without-errors/#findComment-1126546 Share on other sites More sharing options...
trq Posted October 26, 2010 Share Posted October 26, 2010 Use... if (isset($_POST['checkbox'])) { Instead of.... if (isset($_POST['submit'])) { Quote Link to comment https://forums.phpfreaks.com/topic/216855-foreach-w-if-and-else-on-checkboxes-cannot-get-this-working-without-errors/#findComment-1126547 Share on other sites More sharing options...
OldWest Posted October 26, 2010 Author Share Posted October 26, 2010 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."; } } }; ?> Quote Link to comment https://forums.phpfreaks.com/topic/216855-foreach-w-if-and-else-on-checkboxes-cannot-get-this-working-without-errors/#findComment-1126552 Share on other sites More sharing options...
OldWest Posted October 26, 2010 Author Share Posted October 26, 2010 I also tried if (!isset($_POST['checkbox'])) for the else and that did not work either for testing the isset. Quote Link to comment https://forums.phpfreaks.com/topic/216855-foreach-w-if-and-else-on-checkboxes-cannot-get-this-working-without-errors/#findComment-1126554 Share on other sites More sharing options...
Pikachu2000 Posted October 26, 2010 Share Posted October 26, 2010 You need an if() in there somewhere, or lose the else{}. foreach() { } else { } Quote Link to comment https://forums.phpfreaks.com/topic/216855-foreach-w-if-and-else-on-checkboxes-cannot-get-this-working-without-errors/#findComment-1126646 Share on other sites More sharing options...
OldWest Posted October 26, 2010 Author Share Posted October 26, 2010 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!"; }; ?> Quote Link to comment https://forums.phpfreaks.com/topic/216855-foreach-w-if-and-else-on-checkboxes-cannot-get-this-working-without-errors/#findComment-1126702 Share on other sites More sharing options...
OldWest Posted October 26, 2010 Author Share Posted October 26, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/216855-foreach-w-if-and-else-on-checkboxes-cannot-get-this-working-without-errors/#findComment-1126708 Share on other sites More sharing options...
BlueSkyIS Posted October 26, 2010 Share Posted October 26, 2010 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'"; } ?> /> Quote Link to comment https://forums.phpfreaks.com/topic/216855-foreach-w-if-and-else-on-checkboxes-cannot-get-this-working-without-errors/#findComment-1126716 Share on other sites More sharing options...
Pikachu2000 Posted October 26, 2010 Share Posted October 26, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/216855-foreach-w-if-and-else-on-checkboxes-cannot-get-this-working-without-errors/#findComment-1126718 Share on other sites More sharing options...
OldWest Posted October 26, 2010 Author Share Posted October 26, 2010 oops! I realized my original idea was not working and posted a new thread specifically in relation to this issue.. I guess it can be removed. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/216855-foreach-w-if-and-else-on-checkboxes-cannot-get-this-working-without-errors/#findComment-1126723 Share on other sites More sharing options...
OldWest Posted October 26, 2010 Author Share Posted October 26, 2010 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.. Quote Link to comment https://forums.phpfreaks.com/topic/216855-foreach-w-if-and-else-on-checkboxes-cannot-get-this-working-without-errors/#findComment-1126726 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.