Jump to content

Multiple Checkboxes HTML w/PHP: Error with Foreach


Doc20

Recommended Posts

Hey all..

 

I have been messing around with some code with a form that has checkboxes on them, and the option of selecting more than one checkbox to submit to the database.  However, I keep getting the error," Warning: Invalid argument supplied for foreach() in (mydirectory)".

 

I have followed many tutorials and I don't see anything I am doing differently that they are, so I'm sure this is a simple fix, of something I am overlooking, or I am just horrible at this (new person).

 

Here is the snippet of the HTML code for the checkboxes.

<label>
  <input type="checkbox" name="project_list[]" value="chat"/> 
  Chat Project (age 18+ requirement)</label>
<br />
<label>
  <input type="checkbox" name="project_list[]" value="forums"/>
  Forums Project (age 18+ requirement)</label>
<br />
<label>
  <input type="checkbox" name="project_list[]" value="content"/>
  Content/Editorial Project (postcasts, blogging, etc.)</label>
<br />
<label>
  <input type="checkbox" name="project_list[]" value="technology"  />
  Technology Project</label>
<br />
<label>
  <input type="checkbox" name="project_list[]" value="marketing"/>
  Marketing, Promotions and Publicity</label>
<br />
<label>
  <input type="checkbox" name="project_list[]" value="designartwork"/>
  Design and Artwork</label>

 

Here is the PHP code that I am using to do this.

include 'db.php';

mysql_select_db('skylers_training');

if($_POST['submit'])

{
$project_lists = $_POST['project_list'];

foreach($project_lists as $list); //This is the line the code is getting the error on.

 

And the section of the INSERT MYSQL CODE for this part.

$reginsert = mysql_query("INSERT INTO  ... // the rest of the code (that works fine) follows this.

'$list' // This is the line that is supposed to send data from the checklists to the database. 


 

 

When I submit the form, in addition to the error, nothing is submitted to the database table for the list (everything else works fine).

 

Any insight into this, would be appreciated.

 

Thanks!

 

 

Link to comment
Share on other sites

From memory, checkboxes only appear in $_POST if something was actually checked so you can never assume that $_POST['project_list'] will always exist.

 

Try the following to see what's going on:

 

<?php
if (isset($_POST['submit']))
{
$project_lists = isset($_POST['project_list']) ? $_POST['project_list'] : array();  # this is called a ternary operator

if (is_array($project_lists) && count($project_lists) > 0)
{
	// Sweet. Do 'foreach' statement here.
}
else
{
	echo "Something went wrong.  Take a look at what was posted: <br />";
	print_r($_POST);
	exit;
}
}
?>

 

Hope that helps.

Link to comment
Share on other sites

That fixed the error, but it still isn't submitting all the checkboxes to the database, only submitting the last one checked.

 

So if I check Chat, Forums, and Technology -- only technology is actually submitted.

 

In an ideal world, if I checked the 3 checkboxes, they would all get submitted together, just like (with commas separating them):

 

chat, forums, tech

 

I could make separate database rows for each project, but I was trying to be coding efficient and process them all together instead of separately.

 

Any more help would be appreciated.

Link to comment
Share on other sites

I can't see how you are building your SQL queries in the code you provided but basically you need to convert the array checkbox values into a comma separated list yourself. Something like:

 

<?php
// Assume $project_lists array has been validated and looks like this:
$project_lists = array('chat', 'forums', 'tech');

// Make it into a comma separated list
$project_lists_string = explode(',', $project_lists);  // "chat,forums,tech"

// Do SQL query with new comma-separated value now
// ...
?>

Link to comment
Share on other sites

When I try doing what you gave me, I get the error: 

 

Warning: explode() expects parameter 2 to be string, array given in /home/skylers/public_html/trainingmodsite/regsubmit.php on line 31

 

I will post all the code I have so you can see if there is something I am doing wrong, right, or what not.

 

Here is the code for the HTML form (starting at the checkbox section).

  <p><strong>Do you have an idea about the projects you might be interested in?</strong><br />
    <em>Please let us know if there is a specific part of the site that you are interested in contributing towards.</em><br />
<br />
<label>
  <input type="checkbox" name="project_list[]" value="chat"/> 
  Chat Project (age 18+ requirement)</label>
<br />
<label>
  <input type="checkbox" name="project_list[]" value="forums"/>
  Forums Project (age 18+ requirement)</label>
<br />
<label>
  <input type="checkbox" name="project_list[]" value="content"/>
  Content/Editorial Project (postcasts, blogging, etc.)</label>
<br />
<label>
  <input type="checkbox" name="project_list[]" value="technology"  />
  Technology Project</label>
<br />
<label>
  <input type="checkbox" name="project_list[]" value="marketing"/>
  Marketing, Promotions and Publicity</label>
<br />
<label>
  <input type="checkbox" name="project_list[]" value="designartwork"/>
  Design and Artwork</label>
  </p>
  <p>
    <input type="submit" name="submit" id="submit" value="Submit" />
  </p>
</form>

 

 

Here is the processing file, regsubmit.php

<?

include 'db.php';

mysql_select_db('skylers_training');

if (isset($_POST['submit']))
{
$project_lists = isset($_POST['project_list']) ? $_POST['project_list'] : array();  # this is called a ternary operator

if (is_array($project_lists) && count($project_lists) > 0)
{

foreach($project_lists as $list);	


}	


else
{
	echo "Something went wrong.  Take a look at what was posted: <br />";
	print_r($_POST);
	exit;
}

// Assume $project_lists array has been validated and looks like this:
$project_lists = array('chat', 'forums', 'content', 'technology', 'marketing', 'designartwork');

// Make it into a comma separated list
$project_lists_string = explode(',', $project_lists); 	


$reginsert = mysql_query("INSERT INTO registrations (name, age, username, email, skype, timezone, discussion, meaning, whyjoin, team_purpose, achieve, gyc_change, project_list) VALUES('$_POST[name]','$_POST[age]','$_POST[username]','$_POST[email]','$_POST[skype]','$_POST[timezone]','$_POST[discussion]','$_POST[meaning]','$_POST[whyjoin]','$_POST[team_purpose]','$_POST[achieve]','$_POST[gyc_change]','$project_lists_string')") or die(mysql_error());





/*if (!mysql_query($reginterest))
  {
  die('Error: ' . mysql_error()); 

}*/
echo "
   <script type='text/javascript'>
       alert(\"Your Registration of Interest has been successfully submitted.  Please allow for up to 30 days to hear back from us.\");
   </script>
	";
mysql_close();


}

?>

 

I appreciate the help I have been given so far.  I'm still relatively new to PHP (not taken a class in my life -- trying to learn things by myself).

 

Hope all the code can help you guys figure out why the error message and provide a fix for it not submitting the data properly.

 

Here is what I want the PHPMyAdmin to look like on a successful submission.

 

 

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

Changing it to implode fixed the error, I now get no error!

 

However, when I check was submitted, the value submitted into the database is simply just "array"

 

Here is my processing code again.

<?

include 'db.php';

mysql_select_db('skylers_training');

if (isset($_POST['submit']))
{
$project_lists = isset($_POST['project_list']) ? $_POST['project_list'] : array();  # this is called a ternary operator

if (is_array($project_lists) && count($project_lists) > 0)
{

foreach($project_lists as $list);	


}	


else
{
	echo "Something went wrong.  Take a look at what was posted: <br />";
	print_r($_POST);
	exit;
}

// Assume $project_lists array has been validated and looks like this:
$project_lists = array($_POST['project_list']);

// Make it into a comma separated list
$project_lists_string = implode(',', $project_lists); 	


$reginsert = mysql_query("INSERT INTO registrations (name, age, username, email, skype, timezone, discussion, meaning, whyjoin, team_purpose, achieve, gyc_change, project_list) VALUES('$_POST[name]','$_POST[age]','$_POST[username]','$_POST[email]','$_POST[skype]','$_POST[timezone]','$_POST[discussion]','$_POST[meaning]','$_POST[whyjoin]','$_POST[team_purpose]','$_POST[achieve]','$_POST[gyc_change]','$project_lists_string')") or die(mysql_error());





/*if (!mysql_query($reginterest))
  {
  die('Error: ' . mysql_error()); 

}*/
echo "
   <script type='text/javascript'>
       alert(\"Your Registration of Interest has been successfully submitted.  Please allow for up to 30 days to hear back from us.\");
   </script>
	";
mysql_close();


}

?>

 

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

Take a look at this line:

$project_lists = array($_POST['project_list']);

You're inserting something that should be an array into another array.

 

Actually, you should just get rid of that line, since you're setting it correctly further up in the code.

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.