php email form and checkboxes

I've created a subscription form which gathers subscribers names and details etc and adds them to a database. It also some checkboxes at the end for gathering info about what publications the user reads:

<input type="checkbox" name="Magazines[]" value="Surrey Life" />
        Surrey Life</label>
      <br />
      <label>
        <input type="checkbox" name="Magazines[]" value="The Elmbridge" />
        The Elmbridge Magazine</label>
      <br />

etc.

I've created a script which emails the form owner with the subsribers information. It also passes on the magazines that they read in the form of an array. This works but if no checkboxes are checked it returns an error message. I'm not that familiar with forms and checkboxes etc so, if someone could tell me what I need to add to correct this I'd be very grateful.

//Sending Email to form owner
$header = "From: admin@mycompany.com\n"
  . "Reply-To: admin@mycompany.com";
$subject = "Mailing List";
$email_to = "admin@mycompany.com";
foreach($_POST['Magazines'] as $value) {
$check_msg .="$value\n";
}
$body = "The following person has just been added to the mailing list\n"
. "They read the following magazines: \n $check_msg"
;
@mail($email_to, $subject ,$body ,$header ) ;
DallySPAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WarpsehCommented:
Could you tell me the error message?
0
hieloCommented:
Try:
//Sending Email to form owner
$header = "From: admin@mycompany.com\n"
  . "Reply-To: admin@mycompany.com";
$subject = "Mailing List";
$email_to = "admin@mycompany.com";
if( isset($_POST['Magazines']) )
{
   foreach($_POST['Magazines'] as $value) {
   $check_msg .="$value\n";
   }
}
0
Yuval_ShohatCommented:
i'd just add an if condition saying something like:

if ((isset($_POST['magazines'])) && ($_POST['magazines'] !=''))

so that it would look like the code below.

hope this helps.

-=Yuval=-
if ((isset($_POST['magazines'])) && ($_POST['magazines'] !=''))
{
foreach($_POST['Magazines'] as $value) {
$check_msg .="$value\n";
}
 
}

Open in new window

0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

hernst42Commented:
You could try:
if (empty($_POST['Magazines'])) {
  $check_msg = 'none';
} else {
  foreach($_POST['Magazines'] as $value) {
    $check_msg .="$value\n";
  }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DallySPAuthor Commented:
It says:

Warning: Invalid argument supplied for foreach() in /home/sites/mysite.com/public_html/subscriptions/voucher.php on line 75
0
WarpsehCommented:
I think this would be better:
if ( (isset($_POST['magazines'])) && is_array($_POST['magazines'] )) )
{
     foreach....
0
DallySPAuthor Commented:
I've just tried the code that Hernst42 suggested and that works really well. I've substituted  'none' for 'no check boxes selected' which gives the recipient the correct info.

thanks to everyone for a really speedy response.
0
DallySPAuthor Commented:
Thanks again - brilliant. One of these days I'm going to get my head round php!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.