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 ) ;
<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'
$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 ) ;
Could you tell me the error message?
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";
}
}
//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'
$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'
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";
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It says:
Warning: Invalid argument supplied for foreach() in /home/sites/mysite.com/pub lic_html/s ubscriptio ns/voucher .php on line 75
Warning: Invalid argument supplied for foreach() in /home/sites/mysite.com/pub
I think this would be better:
if ( (isset($_POST['magazines'] )) && is_array($_POST['magazines '] )) )
{
foreach....
if ( (isset($_POST['magazines']
{
foreach....
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.
thanks to everyone for a really speedy response.
ASKER
Thanks again - brilliant. One of these days I'm going to get my head round php!!