Forum Moderators: coopster

Message Too Old, No Replies

Passing checkboxe values to mail body

Handling checkboxes in PHP

         

parsi

12:54 am on Jan 17, 2008 (gmt 0)

10+ Year Member



Hi! I'm first timer with php. I've learned little about how to pass values from different formmail objects to php but, can't get it right with chechboxes or selections.

I've made simple formmail with a set of checkboxes and so on: look at this html code
*************************************************************
<td>
<input type="checkbox" name="favorite[]" value="preeches" />
<span class="style36">preeches</span>
<input type="checkbox" name="favorite[]" value="prayers" />
<span class="style36">prayers</span>
<input type="checkbox" name="favorite[]" value="Home meetings" />
<span class="style36">Home meetings</span>
</td>
<td>
<select name="critic[]" multiple size="6">
<option value="preeches">preeches</option>
<option value="prayers">prayers</option>
<option value="Home meetings">Home meetings</option>
</select>
**************************************************************

I want to send values of whatever that is checked or chosen to a mailbody whice again will be sendt to my email. I 've seen lots of codes that made me a little unsecure about using those.
Here is the php script:
*****************************************************************
<?php
$name=addslashes($_POST['name']);
$email=addslashes($_POST['email']);
$contact=addslashes($_POST['contact']);
$favorite=addslashes($_POST['favorite']); here is the problem
$critic=addslashes($_POST['critic']); here is the problem
//I'm not sure about the syntax of an array here

$comments=addslashes($_POST['comments']);


$toemail = "post@example.com";
$subject = "My contact form submission";

$headers = "MIME-Version: 1.0\n"
."From: \"".$name."\" <".$email.">\n"
."Content-type: text/html; charset=UTF-8\n";

$body = "Name: ".$name."<br>\n"
."Email: ".$email."<br>\n"
."Contact: ".$contact."<br>\n"
."Favorite: ".$favorite."<br>\n" (here is the problem)
."Critic: ".$critic[]."<br>\n" (here is the problem)
//and I wouldn't get any results here either
."Comments:<br>\n"
.$comments;

if (!ereg("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $email))
{
echo "That is not a valid email address. Please return to the"
." previous page and try again.";
exit;
}

mail($toemail, $subject, $body, $headers);
echo "Thanks for submitting your comments";
?>
******************************************************************

Could you please give me some guidness.
Thank you

[edited by: eelixduppy at 3:24 am (utc) on Jan. 17, 2008]
[edit reason] example.com [/edit]

dreamcatcher

9:26 am on Jan 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi parsi, welcome to WebmasterWorld. :)

Firstly, the checkboxes and the select POST vars are arrays, so you cannot apply functions to them as you have done:


$favorite=addslashes($_POST['favorite']);
$critic=addslashes($_POST['critic']);

A better solution would be to use array_map [uk3.php.net]. Also, I think what you mean is stripslashes [uk2.php.net], not addslashes. In the case of checkboxes, you`ll need to make sure the array isn`t empty, before you start working with it:


$favorite=(!empty($_POST['favorite'])? array_map('stripslashes',$_POST['favorite']) : '');
$critic=(!empty($_POST['critic'])? array_map('stripslashes',$_POST['critic']) : '');

Then in your mail body, simply implode [uk.php.net] the arrays:

."Favorite: ".(is_array($favorite)? implode(",",$favorite) : '')."<br>\n" (here is the problem)
."Critic: ".(is_array($critic)? implode(",",$critic) : '')."<br>\n" (here is the problem)

Note that I checked that each var was an array using is_array [uk3.php.net] before imploding it. If the string is empty, the implode function would throw an error.

For a better understanding of what is coming in via your POST array use print_r [uk.php.net]:

print_r($_POST);

Hope that helps,
dc