Jump to content

foreach $_POST loop not working


Presto-X

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
Share on other sites

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

Link to comment
Share on other sites

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

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.