Link to home
Start Free TrialLog in
Avatar of DallySP
DallySP

asked on

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 ) ;
Avatar of Warpseh
Warpseh
Flag of Argentina image

Could you tell me the error message?
Avatar of hielo
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";
   }
}
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

ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DallySP
DallySP

ASKER

It says:

Warning: Invalid argument supplied for foreach() in /home/sites/mysite.com/public_html/subscriptions/voucher.php on line 75
I think this would be better:
if ( (isset($_POST['magazines'])) && is_array($_POST['magazines'] )) )
{
     foreach....
Avatar of DallySP

ASKER

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.
Avatar of DallySP

ASKER

Thanks again - brilliant. One of these days I'm going to get my head round php!!