Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

Check Box Problem!

the mail() function

Moderators: macek, egami, gesf

Check Box Problem!

Postby ronwooduk » Sun Jun 24, 2012 2:19 am

Hi Everyone,
I have used PHP Solutions by David Powers to put together a contact form for my web site but I can't get the items in the checkbox to show in my email message. Please could anyone suggest what I need to do to get this to work. I have read the book over and over and tried lots of variations. The book has examples to download but none of these explain or show in simple terms what to do.
Here is the code :

<?php
if (array_key_exists('send', $_POST)) {
$to ='ronwoodtd@gmail.com';
$subject ='Party Enquiry';
$headers='webmaster@jollygoodfun.co.uk';

//list expected fields
$expected = array('first_name', 'last_name', 'telephone', 'email', 'comments', 'entertainment');
// set required fields
$required =array('first_name', 'last_name', 'telephone', 'email', 'entertainment');


// create empty array for missing fields
$missing =array();


// assume nothing is suspect
$suspect = false;
// create a pattern to locate suspect phrases
$pattern = '/Content-Type:|Bcc:|Cc:/i';

// function to check for suspect phrases
function isSuspect($val, $pattern, &$suspect) {
// if the variable is an array, loop through each element
// and pass it recursively back to the same function
if (is_array($val)) {
foreach ($val as $item) {
isSuspect($item, $pattern, $suspect);
}
} else {
// if one of the suspect phrases is found, set Boolean to true
if (preg_match($pattern, $val)) {
$suspect = true;
}
}
}

if ($suspect) {
$mailSent = false;
unset($missing);
}
else {

/* process the variables */
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
// if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
$missing[] = $key;
} elseif (in_array($key, $expected)) {
// otherwise, assign to a variable of the same name as $key
${$key} = $temp;
}
}
}


// go ahead only if not suspect and all required fields ok
if (!$suspect && empty($missing)) {

/* build the message */
$message = "First Name: $first_name\n\n";
$message .= "Last Name: $last_name\n\n";
$message .= "Telephone: $telephone\n\n";
$message .= "Email: $email\n\n";
$message .= "Comments: $comments\n\n";
$message .= "Entertainment: $entertainment";


/* limit line length to 70 characters */
$message =wordwrap($message, 70);

/* send it */
$mailSent = mail($to, $subject, $message, $headers);
if ($mailSent) {
// $missing is no longer needed if email is sent, so unset it
unset($missing);
}
}
}
?>

<?php
require ('templates/docmettacss.html');
?>

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="Send an enquiry to Jolly Good Productions using the form on this page." />
<meta name="keywords" content="enquiry form, enquiry, enquire to Jolly Good Productions" />
<title>Request a quotation from jolly Good Productions</title>



</head>

<?php
require('templates/header.html');
?>

<h1>Request a quotation</h1>

<?php
if ($_POST && isset($missing) && !empty($missing)) {
?>
<p style="color: red">Please complete the missing item(s) indicated.</p>
<?php
}
elseif ($_POST && !mailSent) {
?>
<p style="color: red">Sorry, there was a problem sending your message, please try later.</p>
<?php
}
elseif ($_POST && $mailSent) {
?>
<p><strong>Your message has been sent. Thank you for your enquiry.</strong></p>
<?php } ?>

<div id="contact_form"> <!-- div containing the contact form -->

<form id="feedback" method="post" action="">
<p>
<label for="first_name">First Name: <?php
if (isset($missing) && in_array('first_name', $missing)) { ?>
<span style="warning">Please enter your first name</span><?php } ?>
</label>
<input name="first_name" id="first_name" type="text" class="formbox"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['first_name']).'"';
} ?>
/>

<label for="last_name">Last Name: <?php
if (isset($missing) && in_array('last_name', $missing)) { ?>
<span class="warning">Please enter your last name</span><?php } ?>
</label>
<input name="last_name" id="last_name" type="text" class="formbox"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['last_name']).'"';
} ?>
/>

<label for="telephone">Telephone: <?php
if (isset($missing) && in_array('telephone', $missing)) { ?>
<span class="warning">Please enter your telephone number</span><?php } ?>
</label>
<input name="telephone" id="telephone" type="text" class="formbox"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['telephone']).'"';
} ?>
/>

<label for="email">Email: <?php
if (isset($missing) && in_array('email', $missing)) { ?>
<span class="warning">Please enter your Email</span><?php } ?>
</label>
<input name="email" id="email" type="text" class="formbox"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['email']).'"';
} ?>
/>

<label for="comments">Comments: <?php
if (isset($missing) && in_array('comments', $missing)) { ?>
<span class="warning">Please enter your comments</span><?php } ?>
</label>
<textarea name="comments" id="comments" cols="60" rows="8">
<?php
if (isset($missing)) {
echo htmlentities($_POST['comments']);
} ?></textarea>

</p>

<fieldset id="entertainment">
<h2>Entertainment Required</h2>
<div>
<p><input type="checkbox" name="entertainment[]" value="Chuckle The Clown" id="clown"
<?php
$OK = isset($_POST['entertainment']) ? true : false;
if ($OK && isset($missing) && in_array('Chuckle The Clown', $_POST['entertainment'])) { ?>
checked="checked"
<?php } ?>
/>
<label for="clown">Chuckle The Clown</label>
</p>
<p><input type="checkbox" name="entertainment[]" value="Magic Party" id="magic"
<?php
if ($OK && isset($missing) && in_array('Magic Party', $_POST['entertainment'])) { ?>
checked="checked"
<?php } ?>
/>
<label for="magic">Magic Party</label>
</p>
<p><input type="checkbox" name="entertainment[]" value="Punch and Judy" id="punch"
<?php
if ($OK && isset($missing) && in_array('Punch and Judy', $_POST['entertainment'])) { ?>
checked="checked"
<?php } ?>
/>
<label for="punch">Punch and Judy</label>
</p>
<p><input type="checkbox" name="entertainment[]" value="Pirate Party" id="pirate"
<?php
if ($OK && isset($missing) && in_array('Pirate Party', $_POST['entertainment'])) { ?>
checked="checked"
<?php } ?>
/>
<label for="pirate">Pirate Party</label>
</p>
<p><input type="checkbox" name="entertainment[]" value="Magic and Puppets" id="puppets"
<?php
if ($OK && isset($missing) && in_array('Magic and Puppets', $_POST['entertainment'])) { ?>
checked="checked"
<?php } ?>
/>
<label for="puppets">Magic and Puppets</label>
</p>
<p><input type="checkbox" name="entertainment[]" value="Hazels Jolly Good Fun Party" id="hazel"
<?php
if ($OK && isset($missing) && in_array('Hazels Jolly Good Fun Party', $_POST['entertainment'])) { ?>
checked="checked"
<?php } ?>
/>
<label for="hazel">Hazels Jolly Good Fun Party</label>
</p>

</div>
</fieldset>

<p>
<input name="send" id="send" type="submit" value="Send message" />
</p>
</form>



</div> <!-- End of div id contact form -->

<p class="home">Go to back to <a href="index.php">Home</a> </p>

<?php
require('templates/footer.html');
?>

and this is what it produces as the email:

webmaster@jollygoodfun.co.uk

First Name: John

Last Name: Smith

Telephone: 0123 456789

Email: john@gonehome.com

Comments: test again

Entertainment: Array

Any help would be greatly appreciated.
Regards
Ron
ronwooduk
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 19, 2011 4:23 am

Re: Check Box Problem!

Postby freshnet » Sun Jun 24, 2012 6:43 am

What exactly are you trying to do? Echo out the contents of an array? To do that you'll need to either specify a position in the array, e.g. $entertainment[0] or iterate across the array, e.g.

Code: Select all
foreach ($entertainment as $value)
{
echo $value;
}
User avatar
freshnet
php-forum Active User
php-forum Active User
 
Posts: 278
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: Check Box Problem!

Postby ronwooduk » Sun Jun 24, 2012 8:00 am

So do I put that in the "build message" section ie

$message .= " Entertainment: foreach ($entertainment as $value)
{
echo $value;
}

";

I think it might print out the entire statement rather than just the checked array items? Have I put it in the right place?

I'm trying to build an email message which displays the checked items from my check box choices. Everything is working except the check box part of the message. I'm baffled.

I'll give it a try. I'm setting off to drive to London ( 4 hour drive ) but will have a go later this evening.

Thank you for taking the time to help me.

Regards
Ron
ronwooduk
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 19, 2011 4:23 am

Re: Check Box Problem!

Postby ronwooduk » Sun Jun 24, 2012 1:18 pm

Hi,
I've done this:

// go ahead only if not suspect and all required fields ok
if (!$suspect && empty($missing)) {

/* build the message */
$message = "First Name: $first_name\n\n";
$message .= "Last Name: $last_name\n\n";
$message .= "Telephone: $telephone\n\n";
$message .= "Email: $email\n\n";
$message .= "Comments: $comments\n\n";
$message .= "Entertainment: foreach ($entertainment as $value)
{
echo $value;
}";

and got this :

webmaster@jollygoodfun.co.uk

First Name: Ronald

Last Name: Woodski

Telephone: 0123 456789

Email: fred@btconnect.com

Comments: test

Entertainment: foreach (Array as Send message)
{
echo Send message;
}

Where do I need to put for each loop for it to work please?

Regards
Ron
ronwooduk
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 19, 2011 4:23 am

Re: Check Box Problem!

Postby ronwooduk » Sun Jun 24, 2012 1:39 pm

I tried this:

/* build the message */
$message = "First Name: $first_name\n\n";
$message .= "Last Name: $last_name\n\n";
$message .= "Telephone: $telephone\n\n";
$message .= "Email: $email\n\n";
$message .= "Comments: $comments\n\n";
$message .= "Entertainment: $entertainment";

foreach ($entertainment as $value)
{
echo $value;
}

and got this:

First Name: fred

Last Name: Blogs

Telephone: 0123 456789

Email: fred@btconnect.com

Comments: test

Entertainment: Array

Any ideas?
ronwooduk
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 19, 2011 4:23 am

Re: Check Box Problem!

Postby ronwooduk » Mon Jun 25, 2012 12:03 pm

Thanks freshnet. You were right!

I found another web site which explained how to put the for each loop into the message. See http://stackoverflow.com/questions/5553486/emailing-the-results-of-a-series-of-checkboxes-from-a-contact-form

My code is as follows:

/* build the message */
$message = "First Name: $first_name\n\n";
$message .= "Last Name: $last_name\n\n";
$message .= "Telephone: $telephone\n\n";
$message .= "Email: $email\n\n";
$message .= "Comments: $comments\n\n";
$message .= "Entertainment: \n";
foreach($_POST['entertainment'] as $value){
$message .= $value . "\n";
}

The values from the check boxes now display in my email.

Thanks for your help,
Regards
Ron
ronwooduk
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 19, 2011 4:23 am

Re: Check Box Problem!

Postby freshnet » Tue Jun 26, 2012 9:46 am

yeah that looks much better. You may also want to consider sanitizing your $_POST variables, e.g.

Code: Select all
$var = addslashes($_POST['var']);
User avatar
freshnet
php-forum Active User
php-forum Active User
 
Posts: 278
Joined: Tue Feb 22, 2011 8:19 am
Location: Canada

Re: Check Box Problem!

Postby ronwooduk » Tue Jun 26, 2012 5:47 pm

Ok, thanks.
ronwooduk
New php-forum User
New php-forum User
 
Posts: 14
Joined: Tue Apr 19, 2011 4:23 am


Return to PHP coding => Mail

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.