Link to home
Create AccountLog in
Avatar of imagekrazy
imagekrazy

asked on

In my php send mail form i need to have the check boxes written in my sendmail php form


my form has radio buttons to choose from how do i  structure the check boxes  to be sent to me in my sendmail php form, i know how to get all the other information, but i don't know how to write the code to get one of the checked boxes in the php file.

<form action="sendmail.php" method="POST" enctype="application/x-www-form-urlencoded" onsubmit="return checkform();">
<p><strong>Business Name:</strong><br/></p>
<input type="text" size"25" id="name" name="name"/><br /><br />
<p><strong>What state is your business located:</strong><br />
<select name="state">
      <option value="AL">AL</option>
      <option value="AK">AK</option>
      <option value="AZ">AZ</option>
      <option value="AR">AR</option>
      <option value="CA">CA</option>
      <option value="CO">CO</option>
      <option value="CT">CT</option>
      <option value="DE">DE</option>
      <option value="DC">DC</option>
      <option value="FL">FL</option>
      <option value="GA">GA</option>
      <option value="HI">HI</option>
      <option value="ID">ID</option>
      <option value="IL">IL</option>
      <option value="IN">IN</option>
      <option value="IA">IA</option>
      <option value="KS">KS</option>
      <option value="KY">KY</option>
      <option value="LA">LA</option>
      <option value="ME">ME</option>
      <option value="MD">MD</option>
      <option value="MA">MA</option>
      <option value="MI">MI</option>
      <option value="MN">MN</option>
      <option value="MS">MS</option>
      <option value="MO">MO</option>
      <option value="MT">MT</option>
      <option value="NE">NE</option>
      <option value="NV">NV</option>
      <option value="NH">NH</option>
      <option value="NJ">NJ</option>
      <option value="NM">NM</option>
      <option value="NY">NY</option>
      <option value="NC">NC</option>
      <option value="ND">ND</option>
      <option value="OH">OH</option>
      <option value="OK">OK</option>
      <option value="OR">OR</option>
      <option value="PA">PA</option>
      <option value="RI">RI</option>
      <option value="SC">SC</option>
      <option value="SD">SD</option>
      <option value="TN">TN</option>
      <option value="TX">TX</option>
      <option value="UT">UT</option>
      <option value="VT">VT</option>
      <option value="VA">VA</option>
      <option value="WA">WA</option>
      <option value="WV">WV</option>
      <option value="WI">WI</option>
      <option value="WY">WY</option>
</select>



<br /><br />
<p><strong>E-Mail address:</strong></p>
<input type="text" size="25" id="email" name="email"/> <br /><br />
<p><strong>What does your business need:</strong></p>
<input type="radio" name="group1" value="SEO"checked> SEO/SEM<br>
<input type="radio" name="group1" value="webDesign" >Web Design<br>
<input type="radio" name="group1" value="logo"> Logo<br />
<input type="radio" name="group1" value="update"> Up Date Web Site<br />
<p><strong>A little more detail :</strong><br/>
<textarea name="message" id="message"  rows="5"  cols="55"></textarea>
<p><input type="submit" value="Send"/><input type="reset" value="Reset">
</form>

my send mail php  code;
<?php
echo "<p>Thank you, <b>".$_POST["name"]."</b>, for your message!</p>";
echo "<p>Your e-mail address is: <b>".$_POST["email"]."</b>.</p>";


echo "<p>Your state is: <b>".$_POST["state"]."</b>.</p>";

//echo "<p>Your message was:<br />";
//echo $_POST["message"]."</p>";

//start building the mail string
$msg =  "Name:     ".$_POST["name"]."\n";
$msg .= "E-Mail:   ".$_POST["email"]."\n";
$msg .= "phone:   ".$_POST["phone"]."\n";

$msg .= "state:   ".$_POST["state"]."\n";


//set up e-mail

$recipient = "circleimage@gmail.com";
$subject =   "Form Submission Results";
$mailheaders="From my webiste\n";
$mailheaders .="Reply-To: ".$_POST["email"];

//send the mail
mail($recipient, $subject, $msg, $mailheaders);
?>







</body>
</html>
Avatar of Loganathan Natarajan
Loganathan Natarajan
Flag of India image

you want to have check boxes in the email message?
something like this,
<?php
echo "<p>Thank you, <b>".$_POST["name"]."</b>, for your message!</p>";
echo "<p>Your e-mail address is: <b>".$_POST["email"]."</b>.</p>";


echo "<p>Your state is: <b>".$_POST["state"]."</b>.</p>";

//echo "<p>Your message was:<br />";
//echo $_POST["message"]."</p>";

//start building the mail string
$msg =  "Name:     ".$_POST["name"]."\n";
$msg .= "E-Mail:   ".$_POST["email"]."\n";
$msg .= "phone:   ".$_POST["phone"]."\n";

$msg .= "state:   ".$_POST["state"]."\n";

$msg .= "<form method=post action='3.php'> 
<table border='0' cellspacing='0' width='50%' > 
<tr bgcolor='#ffffff'> 
<td ><input type=checkbox name=box[] value='1'></td> 
<td >&nbsp;check box 1</td> 
<td ><input type=checkbox name=box[] value='2'></td> 
<td >&nbsp;check box 2</td> 
<td ><input type=checkbox name=box[] value='3'></td> 
<td >&nbsp;check box 3</td> 
</tr> 
<tr bgcolor='#f1f1f1'> 
<td ><input type=checkbox name=box[] value='4'></td> 
<td >&nbsp;check box 4</td> 
<td ><input type=checkbox name=box[] value='5'></td> 
<td >&nbsp;check box 5</td> 
<td ><input type=checkbox name=box[] value='6'></td> 
<td >&nbsp;check box 6</td> 
</tr> 

<tr><td colspan =6 align=center><input type=submit value=Select></form></td></tr> 
</td>
</td>
</form>";

//set up e-mail

//echo $msg;

$recipient = "circleimage@gmail.com";
$subject =   "Form Submission Results";
$mailheaders="From my webiste\n";
$mailheaders .="Reply-To: ".$_POST["email"];

//send the mail
mail($recipient, $subject, $msg, $mailheaders);
?>

Open in new window

you can retrieve the values like this,

<?php
// This is to collect box array value as global_variables is set off in PHP5 by default

$box=$_POST['box'];

while (list ($key,$val) = @each ($box)) {
echo "$val,";
}

?>
Avatar of imagekrazy
imagekrazy

ASKER

Hi, I just want  the results of the choice that the  user checked sent  to me with my other information:)
Go into the action script "sendmail.php" and put these 3 lines right at the top.  Then use the form to submit the information, and post the output back here in the code snippet.  We can show you how to find radio and checkbox controls that are activated, but if the client does not select one, you have to use a little trickery to get a value sent to the action script.
echo "<pre>";
var_dump($_POST);
die();

Open in new window

We will probably arrive at something like this:

<input type="radio" name="group1" value="SEO" checked> SEO/SEM<br>

Generates a field named $_POST["group1"] with a value of "SEO"
as Ray said , you can retrieve the value using POST method ...  $rad_value = $_POST["group1"];


<?php
echo "<p>Thank you, <b>".$_POST["name"]."</b>, for your message!</p>";
echo "<p>Your e-mail address is: <b>".$_POST["email"]."</b>.</p>";


echo "<p>Your state is: <b>".$_POST["state"]."</b>.</p>";

//echo "<p>Your message was:<br />";
//echo $_POST["message"]."</p>";

//start building the mail string
$msg =  "Name:     ".$_POST["name"]."\n";
$msg .= "E-Mail:   ".$_POST["email"]."\n";
$msg .= "phone:   ".$_POST["phone"]."\n";

$msg .= "state:   ".$_POST["state"]."\n";
$msg .= "What does your business need: ".$_POST[["group1"]."\n";

//set up e-mail

$recipient = "circleimage@gmail.com";
$subject =   "Form Submission Results";
$mailheaders="From my webiste\n";
$mailheaders .="Reply-To: ".$_POST["email"];

//send the mail
mail($recipient, $subject, $msg, $mailheaders);
?>

Open in new window

its not working,its not going to my email, this is what i am getting;

array(5) {
  ["name"]=>
  string(3) "sss"
  ["state"]=>
  string(2) "AL"
  ["email"]=>
  string(2) "hh"
  ["group1"]=>
  string(4) "logo"
  ["message"]=>
  string(3) "jjj"
}

here is my code that i updated from your help

my php file;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>sending e-mail form</title>

<?php
echo "<pre>";
var_dump($_POST);
die();


echo "<p>Thank you, <b>".$_POST["name"]."</b>, for your message!</p>";
echo "<p>Your e-mail address is: <b>".$_POST["email"]."</b>.</p>";
echo $_POST["message"]."</p>";

echo "<p>Your state is: <b>".$_POST["state"]."</b>.</p>";


//start building the mail string
$msg =  "Name:     ".$_POST["name"]."\n";
$msg .= "E-Mail:   ".$_POST["email"]."\n";
$msg .= "phone:   ".$_POST["phone"]."\n";

$msg .= "state:   ".$_POST["state"]."\n";
$msg .= "What does your business need: ".$_POST["group1"]."\n";
$msg .= "Message:  ".$_POST["message"]."\n";
//set up e-mail

$recipient = "circleimage@gmail.com";
$subject =   "Form Submission Results";
$mailheaders="From my webiste\n";
$mailheaders .="Reply-To: ".$_POST["email"];

//send the mail
mail($recipient, $subject, $msg, $mailheaders);
?>
</body>
</html>

my html form;

<form action="sendmail2.php" method="POST" enctype="application/x-www-form-urlencoded" onsubmit="return checkform();">
<p><strong>Business Name:</strong><br/></p>
<input type="text" size"25" id="name" name="name"/><br /><br />
<p><strong>What state is your business located:</strong><br />
<select name="state">
      <option value="AL">AL</option>
      <option value="AK">AK</option>
      <option value="AZ">AZ</option>
      <option value="AR">AR</option>
      <option value="CA">CA</option>
      <option value="CO">CO</option>
      <option value="CT">CT</option>
      <option value="DE">DE</option>
      <option value="DC">DC</option>
      <option value="FL">FL</option>
      <option value="GA">GA</option>
      <option value="HI">HI</option>
      <option value="ID">ID</option>
      <option value="IL">IL</option>
      <option value="IN">IN</option>
      <option value="IA">IA</option>
      <option value="KS">KS</option>
      <option value="KY">KY</option>
      <option value="LA">LA</option>
      <option value="ME">ME</option>
      <option value="MD">MD</option>
      <option value="MA">MA</option>
      <option value="MI">MI</option>
      <option value="MN">MN</option>
      <option value="MS">MS</option>
      <option value="MO">MO</option>
      <option value="MT">MT</option>
      <option value="NE">NE</option>
      <option value="NV">NV</option>
      <option value="NH">NH</option>
      <option value="NJ">NJ</option>
      <option value="NM">NM</option>
      <option value="NY">NY</option>
      <option value="NC">NC</option>
      <option value="ND">ND</option>
      <option value="OH">OH</option>
      <option value="OK">OK</option>
      <option value="OR">OR</option>
      <option value="PA">PA</option>
      <option value="RI">RI</option>
      <option value="SC">SC</option>
      <option value="SD">SD</option>
      <option value="TN">TN</option>
      <option value="TX">TX</option>
      <option value="UT">UT</option>
      <option value="VT">VT</option>
      <option value="VA">VA</option>
      <option value="WA">WA</option>
      <option value="WV">WV</option>
      <option value="WI">WI</option>
      <option value="WY">WY</option>
</select>



<br /><br />
<p><strong>E-Mail address:</strong></p>
<input type="text" size="25" id="email" name="email"/> <br /><br />
<p><strong>What does your business need:</strong></p>
<input type="radio" name="group1" value="SEO"checked> SEO/SEM<br>
<input type="radio" name="group1" value="webDesign" >Web Design<br>
<input type="radio" name="group1" value="logo"> Logo<br />
<input type="radio" name="group1" value="update"> Up Date Web Site<br />
<p><strong>A little more detail :</strong><br/>
<textarea name="message" id="message"  rows="5"  cols="55"></textarea>
<p><input type="submit" value="Send"/><input type="reset" value="Reset">
</form>
Please use the code snippet when you post code here at EE.  It makes it easier to read and we get line numbers, which makes for fluent conversation in the back-and-forth dialog that we have.

According to this:
array(5) {
  ["name"]=>
  string(3) "sss"
  ["state"]=>
  string(2) "AL"
  ["email"]=>
  string(2) "hh"
  ["group1"]=>
  string(4) "logo"
  ["message"]=>
  string(3) "jjj"
}
When the form was filled in, the radio button for "Logo" was checked.  So I guess the next thing we need to see is the ENTIRE contents of the email you received.  Have you posted the current email script?  Have you checked to see that you are running the correct email script (action="sendmail2.php") from the correct directory?
HI,
this is the action;
<form action="sendmail2.php" method="POST" enctype="application/x-www-form-urlencoded" onsubmit="return checkform();">

and my file name is sendmail2.php
but i am not getting the  out put
It works , when i took out the code

 echo "<pre>";
var_dump($_POST);
die();

but can you again tell me what i need to write in the php for the user leaving something blank:)
Yes, that makes sense.  This instruction:

die();

... is intended to stop the script, so we would have time to read the output of var_dump().

Now that we are past that, here is a teaching example of how to deal with un-selected radio buttons.
<?php // RAY_radio_default_value.php
error_reporting(E_ALL);


// THIS DEMONSTRATES A WAY TO ASSIGN A DEFAULT VALUE TO A RADIO BUTTON
// THE HIDDEN CONTROL MUST PRECEDE THE RADIO BUTTONS
// THE HIDDEN VALUE WILL APPEAR IN THE POST DATA IF A SELECTION IS NOT MADE


// IF ANYTHING WAS POSTED
if (!empty($_POST))
{
    // SHOW THE POST DATA
    var_dump($_POST);

    // IF THE 'radio_control' WAS FILLED IN
    if (isset($_POST["radio_control"]))
    {
        // TEST FOR THE ALLOWABLE OPTIONS
        switch ($_POST["radio_control"])
        {
            case "none"  : echo "<br/>YOU DID NOT MAKE A CHOICE";
                           break;
            case "yes"   : echo "<br/>YOU CHOSE 'YES'";
                           break;
            case "no"    : echo "<br/>YOU CHOSE 'NO'";
                           break;
            case "maybe" : echo "<br/>YOU CHOSE 'MAYBE'";
                           break;
            default      : echo "<br/>RUN LIKE HELL! THIS WEB SITE IS UNDER ATTACK";
        }
    }
}
// END OF PROCESSING - PUT UP THE FORM
$form = <<<FORM
<form method="post">
CHOOSE:
<br/><input type="hidden" name="radio_control" value="none"  />
<br/><input type="radio"  name="radio_control" value="yes"   />Yes
<br/><input type="radio"  name="radio_control" value="no"    />No
<br/><input type="radio"  name="radio_control" value="maybe" />Maybe

<br/><input type="submit" name="submit_button" value="Go!" />
</form>
FORM;
echo $form;

Open in new window

wow that is cool, so that will allow the user to check something, but what about if they leave something blank, like there name email address etc.., the reason I am asking is, i  am getting  empty forms sent to my email with nobody filling in  anything , do you know why? is it just spiders   or some one being a dumb ass!
thanks
It's spiders or it's someone being a jerk.  Could be any of those.  Form-to-email scripts are regular targets of attack.

The general guidance when dealing with external input is "Accept only known good values" and this means filtering the input data after the form has been submitted.  Here is my teaching example of a form-to-email script.  Starting after line 30, you can add tests to see if the variables have been filled in.  If they have not, you might put the form up again with some kind of an error message.  If nothing had been submitted, you might just discard the request without sending the email.  You could know that if you did something like this:

// CONCATENATE THE INPUT STRINGS
$x = trim($email) . trim($name) . trim($telephone);
if (empty($x)) die();

Another useful strategy is to put a CAPTCHA image next to the submit button.  That will weed out the spiders, but maybe not the jerks ;-)
<?php // RAY_form_to_email.php
error_reporting(E_ALL);

// SEND MAIL FROM A FORM

// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    $str = stripslashes($str);
    $str = trim(preg_replace("/ +/", " ", $str));
    $str = preg_replace('/^ a-zA-Z0-9&+:?_\.\-/', '', $str);
    return $str;
}

// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "NoReply@Your.org";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS
$to[]  = "You@Your.org";
$to[]  = "Her@Your.org";
$to[]  = "Him@Your.org";

// IF THE DATA HAS BEEN POSTED
if (!empty($_POST['email']))
{
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $email      = clean_string($_POST["email"]);
    $name       = clean_string($_POST["name"]);
    $telephone  = clean_string($_POST["telephone"]);

    // CONSTRUCT THE MESSAGE
    $content    = '';
    $content   .= "You have a New Query From $name \n\n";
    $content   .= "Tel No: $telephone\n";
    $content   .= "Email: $email\n";

    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
    }
} // END OF PHP - PUT UP THE FORM
?>
<form method="post">
<br/>Email: <input name="email" />
<br/>Phone: <input name="telephone" />
<br/>Name:  <input name="name" />
<br/><input type="submit" />
</form>

Open in new window

Okay, this will probably sound stupid, I am new to all of this so bear with me,

okay this is what i did from your  code;

<?php //
error_reporting(E_ALL);

// SEND MAIL FROM A FORM

// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    $str = stripslashes($str);
    $str = trim(preg_replace("/ +/", " ", $str));
    $str = preg_replace('/^ a-zA-Z0-9&+:?_\.\-/', '', $str);
    return $str;
}

// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "circleimage@gmail.com";
$subj  = "Contact Form";  // do I change this code?

// THIS IS AN ARRAY OF RECIPIENTS
$to[]  = "You@Your.org";  // what do i change this to? is it my email address?
$to[]  = "Her@Your.org";  // and do i leave this as is?
$to[]  = "Him@Your.org";  // this too?

// IF THE DATA HAS BEEN POSTED
if (!empty($_POST['email']))
{
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $email      = clean_string($_POST["email"]);
    $name       = clean_string($_POST["name"]);
    $state      = clean_string($_POST["state"]);
    $messages   = clean_string($_POST["messages"]);
    $group1     = clean_string($_POST["group1"]);  // is this right? for the radio buttons
      
      
    // CONSTRUCT THE MESSAGE
    $content    = '';
    $content   .= "You have a New Query From Business $name \n\n";
    $content   .= "state: $state\n";
    $content   .= "Email: $email\n";
    $content   .= "Choose one: $group1\n";  // is these also right
    $content   .= "Message None: $messages\n";  // this too?
    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
    }
} // END OF PHP - PUT UP THE FORM
?>

(and do i add this whole code to my sendmail2. or do i use what you showed me after correction of course, as the out put and the validation  )
wow that is so cool , it worked, but my final question is , if some leaves it blank  or spider  does something, it will show a blank page and not a message, cause i l didn't fill everything out and submit it, and i got a blank message, and no email:) Is that suppose to happen?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Thank you , you  have been a great help!
Thanks for the points.  If you want to get stronger in PHP, you might enjoy this book:
http://www.sitepoint.com/books/phpmysql4/

Happy New Year, ~Ray