Jump to content

[SOLVED] Sending multiple checkboxes to email


rcle

Recommended Posts

Hello everybody,

 

I'm new at this forum. I'm trying to post the value of multiple checkboxes to an email, but in my output email, i always get the type array for my checkbox value. Here you can see the php code below :

I succeed to get the print of checkboxes on the "thank you" php page, but i'm having problems to send them to the email field.

Thank you for any help guys!!!!!

-----------------------------------------------------------------------------------------------------------------------------

<?php

 

$my_email = "[email protected]";

$continue = "/";

$product = "";

if ($_SERVER['REQUEST_METHOD'] != "POST"){exit;}

 

function new_line_check($a)

{

 

if(preg_match('`[\r\n]`',$a)){header("location: $_SERVER[HTTP_REFERER]");exit;}

 

 

}

new_line_check($_POST['field1']);

new_line_check($_POST['field3']);

 

 

 

print ("You ordered the following products:<br />");

 

if(is_array($_POST['field2']))

{

foreach($_POST['field2'] as $product)

{

print ("$product<br />");

}

}

 

$product = implode(', ', $_POST['field2']);

$message = "";

 

 

foreach($_POST as $key => $value){if(!(empty($value))){$set=1;}$message = $message . "$key: $value\n\n";} if($set!==1){header("location: $_SERVER[HTTP_REFERER]");exit;}

 

$message = $message ;

$message = stripslashes($message);

 

$subject = "inschrijving ";

$headers = "From: " . $_POST['field3'] . "\n" . "Return-Path: " . $_POST['field3'] . "\n";

 

mail($my_email,$subject,$message,$headers,$_POST['field3']);

?>

 

<!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><title>test</title></head>

<body>

<div align="left"><span class="style1"><b>Thanx for your order <?php print stripslashes($_POST['field3']); ?>!</b>

  <br/>

  We are managing your order right now.</span>

  </div>

</body>

</html>[/b][/b]

-------------------------------------------------------------------------------------------------------------------------------

Hello,

 

This is the html code. Thank you in advance.

 

-----------------------------------------------------------------------------

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<script src="prototype.js" type="text/javascript"></script>

<script type="text/javascript" src="validation.js"></script>

<link rel="stylesheet" type="text/css" href="style.css" />

</head>

<body>

<form id="test" action="multipleselections.php" method="post">

    <fieldset>

      <legend>Orderformulier</legend>

      <div class="form-row">

      <div class="field-label"><label for="field1">Naam</label>:</div>

      <div class="field-widget"><input name="field1" id="field1" class="required" title="Vul hier uw naam in" /></div>

      </div>

  <div class="form-row">

      <div class="field-label"><label for="field2">Overzicht producten:</label>:</div>

      <div class="field-label">

        <input type="checkbox" name="field2[]" id="field2-prcode001" value="product001" />product001<br />

        <input type="checkbox" name="field2[]" id="field2-prcode002" value="product002" />product002<br />

<input type="checkbox" name="field2[]" id="field2-prcode003" value="product003" />product003<br />

<input type="checkbox" name="field2[]" id="field2-prcode004" value="product004" class="validate-one-required" />product004<br />

      </div>

      </div>

      <div id="email-signup" class="form-row" style="display:true;">

  <div class="field-label"><label for="field3">Email adres</label>:</div>

      <div class="field-widget"><input name="field3" id="field3" class="required validate-email" title="Vul hier een geldig emailadres in" /></div>

      </div>

    </fieldset>

    <input type="submit" value="Verzend order" /> <input type="button" value="Reset" onclick="valid.reset(); return false" />

    </form>

    <script type="text/javascript">

function formCallback(result, form) {

      window.status = "valiation callback for form '" + form.id + "': result = " + result;

      }

     

      var valid = new Validation('test', {immediate : true, onFormValidate : formCallback});

    </script>

</body>

</html>

Checkboxes when evaluating to true (read: checked) return an array

 

<?php
  $x = count($_POST['field2']);
  
  for($i=0;$i<$x;$i++)
  {
    $message .= 'You ordered product ' . $_POST['field2'][$i];
  }
?>

 

See return vals of Your Checkboxes

Thank you,

 

I have still problems though. Now i recieve in my email following text :

------------------------------------

" You ordered product product001You ordered product product003You ordered product product004field1: raf

 

field2: Array

 

field3: [email protected] "

 

------------------------------------

 

Field 2 still gives the array message and the message repeats every time.

 

Thanx in advance

I have looked through your code and reorganized it and took out some parts that you were not using yet.  The main issue was that you were treating $_POST['field2'] as an associative array instead of a regular array.  You can't key => value on arrays using numerical indexes as far as I know.  I also tab formatted your code for readability.  This structure I give you is a little easier to understand I hope.  Put your email in the $to field, and replace your multipleselections.php with mine and test it :)  Keep your old copy in case you don't like mine!

 

multipleselections.php

<?php
  //Setup Email
  $to      = '[email protected]'; //## CHANGE TO YOUR INBOX ##
  $subject = "inschrijving ";// message subject
  $message = '';//initialize as blank
  $products = '';//initialize as blank
  $headers = "From: " . $_POST['field3'] . "\r\n" . "Return-Path: " . $_POST['field3'] . "\r\n";
  //setup email

//FUNCTION
function new_line_check($a)
{
  if(preg_match('`[\r\n]`',$a)){header("location: $_SERVER[HTTP_REFERER]");exit;}
}//nlc

//Make sure page is being accessed correctly
if ($_SERVER['REQUEST_METHOD'] != "POST")
{
  exit();
}

//validate $_POST vars
new_line_check($_POST['field1']);
new_line_check($_POST['field3']);

//FIELD2 RETURNS A REGULAR ARRAY,NOT ASSOCIATIVE
if(is_array($_POST['field2']))
{
  $x = count($_POST['field2']);
  for($i=0;$i<$x;$i++)
  {
    $products .= "Item $i: " . $_POST['field2'][$i] . "\r\n";
  }
}


//check all post vars have values
foreach($_POST as $key => $value)
{
  if(!(empty($value)))
  {
    $set=1;
  }
}
//if value is empty, return to previous page
if($set!==1)
{
  header("Location: $_SERVER[HTTP_REFERER]");
  exit();
}

//FORMAT MESSAGE AS YOU WANT
$message = "New order from: " . stripslashes($_POST['field3']) . "\r\n\r\n";
$message .= "Customer Name: " . $_POST['field1'] . "\r\n";
$message .= "Date: " . date("F j, Y - G:i") . "\r\n\r\n";
$message .= $products;
$message = stripslashes($message);


//SEND THE EMAIL
mail($to, $subject, $message, $headers);

//OUTPUT MESSAGE TO CLIENT BELOW
?>
<!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><title>test</title></head>
<body>
<div align="left"><span class="style1">Thanx for your order <?php echo stripslashes($_POST['field3']); ?>!
<p>Order:<br />
<?php echo nl2br($products); ?></p>

  We are managing your order right now.</span>
  </div>
</body>
</html>[/b][/b]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.