Presto-X Posted September 12, 2009 Share Posted September 12, 2009 Hello everyone, I am working on a project for a non-profit that helps children with special needs. I have a form with a series of activities for volunteers to sign up for, (the activities are always changing) next to each activity I have a checkbox and at the bottom a submit button, when the form is submitted we are saving this information in to our database, in a table with 4 columns (userid, username, email, options). What I’m trying to do is combine all of the submitted/checked checkboxes and save the names in to the database in the options column. The following code is only displaying the first checkbox, and I’m not sure why, there is most likely a better way of using the following code, if you have any suggestions I’m open to ideas. function values(){ $valueRow = 0; foreach ($_POST as $value): // skip the first 4 values form1, userid, username, email if(($valueRow >= 4)&&(!empty($value))): // clean up and add a line brake return str_replace(array("\\",),"",$value)."\n"; endif; $valueRow++; endforeach; } $insertSQL = sprintf("INSERT INTO jos_schedule_volunteers (userid, username, email, options) VALUES (%s, %s, %s, %s)", GetSQLValueString($_POST['userid'], "double"), GetSQLValueString($_POST['name'], "text"), GetSQLValueString($_POST['email'], "text"), GetSQLValueString(values(), "text")); Quote Link to comment https://forums.phpfreaks.com/topic/173975-foreach-_post-loop-not-working/ Share on other sites More sharing options...
Gayner Posted September 12, 2009 Share Posted September 12, 2009 %s ? Quote Link to comment https://forums.phpfreaks.com/topic/173975-foreach-_post-loop-not-working/#findComment-917088 Share on other sites More sharing options...
RussellReal Posted September 12, 2009 Share Posted September 12, 2009 hey.. why don't you name the checkboxes something like this: <input type="checkbox" name="check[]" value="Agree To Donate Star Stickers" /> then instead of 'skipping' the first 4.. you just loop through $_POST['check'] with a foreach.. much less hassle Quote Link to comment https://forums.phpfreaks.com/topic/173975-foreach-_post-loop-not-working/#findComment-917093 Share on other sites More sharing options...
Presto-X Posted September 12, 2009 Author Share Posted September 12, 2009 Thanks for the reply Russell, This is my updated code, it's still only showing one of the checkboxes values function values(){ foreach ($_POST['check'] as $value): if(!empty($value)): return str_replace(array("\\",),"",$value)."\n"; endif; endforeach; } Quote Link to comment https://forums.phpfreaks.com/topic/173975-foreach-_post-loop-not-working/#findComment-917110 Share on other sites More sharing options...
RussellReal Posted September 12, 2009 Share Posted September 12, 2009 coz 'return' exits the function.. You would have to do something like this.. function values(){ $checks = array(); foreach ($_POST['check'] as $value): if(!empty($value)): $checks[] = str_replace(array("\\",),"",$value)."\n"; endif; endforeach; return $checks; } $getArrayOfCheckboxes = values(); Quote Link to comment https://forums.phpfreaks.com/topic/173975-foreach-_post-loop-not-working/#findComment-917307 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.