Hi, i am trying to make an order form that people select their candy they like and how much they want as well as their address and phone number for the website owner to go through and complete the order, i am having trouble getting the php mail function to send off the checkbox data.

thanks, beerwinespirits

Recommended Answers

All 13 Replies

Hopefully this small example will help:

$wantsCola = !empty($_POST['ColaCheckbox']);

Basically, the variable $wantsCola is true if checked; false if not. Since empty(mixed) returns a boolean (true/false) — if we inverse it with the !, we'll get whether it is checked or not. Here is that, in a longer way:

$wantsCola = true;
if (empty($_POST['ColaCheckbox'])) {
    $wantsCola = false;
}

I made this one just for you by changing one of my programmes.
Index

  <?php
  include "candy.php";?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
    <title>Contact us</title>
    <link rel="STYLESHEET" type="text/css" href="style/fg_membersite.css" />
    <script type='text/javascript' src='scripts/gen_validatorv31.js'></script>
    <link rel="STYLESHEET" type="text/css" href="style/pwdwidget.css" />
    <script src="scripts/pwdwidget.js" type="text/javascript"></script> 

<style>


    #framewidth
    {
        width:500px;
    }


    .colwidth
    {
        display:inline-block;
        width:150px;;
    }

</style> 
</head>
<body>


<!-- Form Code Start -->
<form action="candy.php" method="post">
<fieldset id='framewidth'>
<legend ><font color="#033f64"><h1>Register</h1></font></legend>

<div class='container'>
    <label for='name'  class='colwidth' >Name*: </label>
    <input type='text' name='name' id='name' maxlength="50" /><br/>
    <span id='register_name_errorloc' class='error'></span>
</div>
<div class='container'>
    <label for='parentage' class='colwidth' >Parentage*: </label>
    <input type='text' name='parentage'  id='parentage' maxlength="50"/><br/>
    <span id='register_name_errorloc' class='error'></span>
</div>
<div class='container'>
    <label for='residence' class='colwidth' >Residence*: </label>
    <input type='text' name='residence' id='residence' maxlength="50" /><br/>
    <span id='register_name_errorloc' class='error'></span>
</div>
<div class='container'>
    <label for='email' class='colwidth' >Email*: </label>
    <input type='text' name='email' id='email' maxlength="50" /><br/>
    <span id='register_name_errorloc' class='error'></span>
</div>
<div class='container'>
    <label class='colwidth' for='phoneno' >Phone*: </label>
    <input type='text' name='phoneno' id='phoneno' maxlength="50" /><br/>
    <span id='register_name_errorloc' class='error'></span>
</div>
<div class='container'>
    <label class='colwidth' for='qualification' >Qualification*: </label>
    <input type='text' name='qualification' id='qualification' maxlength="50" /><br/>
    <span id='register_name_errorloc' class='error'></span>
</div>
<!--This is a comment. Comments are not displayed in the browser-->
<div class='container'>
    <label class='colwidth' for='Candy' >Select Candy*: </label>
    <select name = "candy" id="candy"style='width:155px;'value='multiple'>
    <option>Candy1</option>
    <option>Candy2</option>
    <option>Candy3</option>
    <option>Candy4</option>
    <option>Candy5</option>
    <option>Candy6</option>
    </select>

          <span id='register_name_errorloc' class='error'></span>
</div>
<div class='container'>
    <label class='colwidth' for='howmuch' >How Much*: </label>
    <select name = "howmuch" id="howmuch"style='width:155px;'value='multiple'>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    </select>

          <span id='register_name_errorloc' class='error'></span>
</div>
<div class='container' >
    <div  style="display:inline;margin-top:-200px;"><label for='message' class='colwidth'  >Your Query If Any*: </label></div>
    <!--<input type='textarea' name='district' id='district' maxlength="50" style='height:100px;' />-->
    <textarea name='message' id='message' style='height:100px; width:150px; margin-top:3px;' ></textarea><br/>
    <span id='register_name_errorloc' class='error'></span>
</div>

<div style="padding-left:203px">
    <input type='submit' name='formSubmit' value='Submit' />
</div>
</fieldset>
</form>




<script type='text/javascript'>
// <![CDATA[
    var pwdwidget = new PasswordWidget('thepwddiv','password');
    pwdwidget.MakePWDWidget();

    var frmvalidator  = new Validator("register");
    frmvalidator.EnableOnPageErrorDisplay();
    frmvalidator.EnableMsgsTogether();
    frmvalidator.addValidation("name","req","Please provide your name");

    frmvalidator.addValidation("email","req","Please provide your email address");

    frmvalidator.addValidation("email","email","Please provide a valid email address");

    frmvalidator.addValidation("username","req","Please provide a username");

    frmvalidator.addValidation("password","req","Please provide a password");

    frmvalidator.addValidation("tehsil","req","Please provide a tehsil");



// ]]>
</script>



</body>
</html>

Candy.php

<?php
if(is_array($formCourse)) {
  echo "<select name='formCourse' id='formCourse' style='width:40px'>";
  foreach($formCourse as $id => $formCourse) {
    echo "<option value='$id'>$formCourse</option>";
  }
  echo "</select>";
}

if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "Your Form Recieved!";
}
$name = $_POST['name'];
$parentage = $_POST['parentage'];
$residence = $_POST['residence'];
$email = $_POST['email'];
$phoneno = $_POST['phoneno'];
$formCourse = $_POST['formCourse'];
$candy = $_POST['formCourse'];
$howmuch = $_POST['formCourse'];
$qualification = $_POST['qualification'];
$message = $_POST['message'];
$submit = $_POST['submit'];

//Validate first
if(empty($name)||empty($email))
{
    echo "Name and email are mandatory!";
    exit;
}

if(IsInjected($email))
{
    echo "Bad email value!";
    exit;
}

$email_from = "imti321@gmail.com";//<== update the email address
$email_subject = "New Form submission";
$email_body = "New Form Has been submitted by $name.\n".
    "Parentage           :$parentage.\n".
    "Email               :$email.\n".
    "Residence           :$residence.\n".
    "Phone NO            :$phoneno.\n".
    "Candidates Query    :$message.\n".
    "Qualification       :$qualification.\n".
    //"Sex:$sex".
    //"Age:\n $age".
    "Candy Selected     :$candy.\n".
    "howmuch     :$howmuch.\n".

    //Please Click on $email to reply back.

$to = "masoodi@embdia.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
//header('Location: thank-you.html');
echo "Thanks, For Contacting Us.We have recieved your mail at ..... Dept.You will Soon recieve a Phone call or Email From Us";

// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}

?>

If you get into problem please revert back

is there a way to add all the checked items into an array and then display the data into sorted columns in an email template?

as in:

    item          qty
chocolate frog |     3
chocolate egg  |     4

ect.

as i do not want to write seperate code sections for each item, i also want to be able to add new products through an administration panel.

oh that looks neat as imti321, ill check to see if that works

That will defnitly work i have checked it if you get any problem please do come back.

is there a way i can change the drop box into checkboxes and text boxes for the quantitys?

there is a problem with the code, it is not sending the numbers from the drop down lists

YOU HAVE TO CHECK THESE TWO LINES
$candy = $_POST['formCourse'];
$howmuch = $_POST['formCourse'];
AND CHANGE THEN TO
$candy = $_POST['candy'];
$howmuch = $_POST['howmuch'];

is rest of code working or you face anyother problem.?

Here it is but you may have to change some values

<form action="candy.php" method="post">

Which Candy do you want borrow?<br />
<input type="checkbox" name="candy[]" value="A" />candy1<br />
<input type="checkbox" name="candy[]" value="B" />candy2<br />
<input type="checkbox" name="candy[]" value="C" />candy3<br />
<input type="checkbox" name="candy[]" value="D" />candy4<br />
<input type="checkbox" name="candy[]" value="E" />candy5

<input type="submit" name="formSubmit" value="Submit" />

</form>

<?php
  $aCandy = $_POST['candy'];
  if(empty($aCandy)) 
  {
    echo("You didn't select any Candy.");
  } 
  else
  {
    $N = count($aCandy);

    echo("You selected $N candy(s): ");
    for($i=0; $i < $N; $i++)
    {
      echo($aCandy[$i] . " ");
    }
  }


    if(IsChecked('candy','A'))
{
echo "you have.done this"..
}
//or use in a calculation ...

$price += IsChecked('candy','A') ? 10 : 0;
$price += IsChecked('candy','B') ? 20 : 0;
?>

I use: isset($_POST['checkboxName']); to determine checkbox fields.

I gave you check box html and php files you can now easily modify them and use them in previous code

can you show me the error you are getting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.