rbragg Posted May 8, 2007 Author Share Posted May 8, 2007 I still get [cbUnit] => Array. :-X All other values that are not arrays are passed successfully. I have another array from a checkbox group that is doing the same thing. Could this not be caused by the foreach statements in my form? Quote Link to comment Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 Just noticed this: $cbUnitName = array(''Talon', 'Star', 'Cater', 'Landrum'); I would think that would throw a syntax error, if that was not fixed I would try fixing it to this: $cbUnitName = array('Talon', 'Star', 'Cater', 'Landrum'); That and this does not make any sense.... if ( !isset($_POST['cbUnit']) ) $_POST['cbUnit'] = array(); // why this???? That is the issue. { foreach($_POST['cbUnit'] as $index => $checkedUnit) { echo (($unitName == $checkedUnit) ? (' checked="yes"') : (''));; } echo ">" . $unitName . "<br>"; } Try this instead if (isset($_POST['cbUnit']) ) { foreach($_POST['cbUnit'] as $index => $checkedUnit) { echo (($unitName == $checkedUnit) ? (' checked="yes"') : (''));; } echo ">" . $unitName . "<br>"; } Only run through the code if it isset, do not set it to an empty array if it is not. You can also throw in an array check like so: if (isset($_POST['cbUnit']) && is_array($_POST['cbUnit']) ) { foreach($_POST['cbUnit'] as $index => $checkedUnit) { echo (($unitName == $checkedUnit) ? (' checked="yes"') : (''));; } echo ">" . $unitName . "<br>"; }else { echo 'Did not enter array'; // just for testing purposes. } Too avoid some errors. Quote Link to comment Share on other sites More sharing options...
rbragg Posted May 8, 2007 Author Share Posted May 8, 2007 Thank you for your help. I used your last example and still have the same problem. :'( The empty array idea was suggested by a helper earlier in this thread. Also, the extra single quote was only mistyped here and is not represented in my code. Hmmm... I'm running out of things to change. LOL! Quote Link to comment Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 I guess what you should do is post current code (full) and re-iterate the problem so everyone is clear on the issue. Quote Link to comment Share on other sites More sharing options...
rbragg Posted May 8, 2007 Author Share Posted May 8, 2007 Sure! The only form element I'm posting is the cbUnit array to keep you from scrolling to China. Page 1 (user enters info): <?php session_start(); if( $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['confirm']) ) { foreach ($_POST as $key => $value) { if ($key != "confirm") { $_SESSION[$key] = strip_tags($value); } } include 'shared/validate.php'; } ?> <form name="application" method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>"> <?php $cbUnitName = array('Talon', 'Star', 'Cater', 'Landrum'); foreach($cbUnitName as $key => $unitName) { echo "<input type='checkbox' name='cbUnit[]' value='" . $unitName . "'"; if (isset($_POST['cbUnit']) && is_array($_POST['cbUnit']) ) { foreach($_POST['cbUnit'] as $index => $checkedUnit) { echo (($unitName == $checkedUnit) ? (' checked="yes"') : (''));; } echo ">" . $unitName . "<br>"; } else { echo 'Did not enter array'; # just for testing purposes. } }?> <input type="submit" name="confirm" id="confirm" value="confirm" class="style1"> </form> Page 2 (info is echoed): <?php session_start(); echo "<pre>". print_r($_SESSION, true) ."</pre>\n"; $cbUnit = $_SESSION['cbUnit']; foreach($cbUnit as $key => $unit) { echo $unit . "<br>"; }?> The problem is that the array values are not displayed when echoing. Only [cbUnit] => Array and the Warning: Invalid argument supplied for foreach() error. Please don't find something very simple and make me feel like an idiot. I'm very sensitive today. Quote Link to comment Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 <?php session_start(); if( $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['confirm']) ) { foreach ($_POST as $key => $value) { if ($key != "confirm") { if (is_array($value)) { $_SESSION[$key] = $value; }else { $_SESSION[$key] = strip_tags($value); // this might be the problem try } } } include 'shared/validate.php'; } Strip_tags should only take a string, not an array, which in return when it did the array part it took it literaly as if you just printed it. Anytime you print an array it should display Array. Hope that makes sense but give that a shot. Quote Link to comment Share on other sites More sharing options...
rbragg Posted May 8, 2007 Author Share Posted May 8, 2007 It made good sense. This was successful in echoing the contents of the array. However, on the form it would not display $unitName (the name next to the checkbox) unless I did as that previous helper suggested and add: $_POST['cbUnit'] = array(); Also, since the array is no longer stored as a string I am no longer able to implode it for db insertion" $cbUnitString = implode(", ",$unit); Is there a way around this? Quote Link to comment 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.