Jump to content

Recommended Posts

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"));

Link to comment
https://forums.phpfreaks.com/topic/173975-foreach-_post-loop-not-working/
Share on other sites

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 :)

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;
}

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();

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.