I created a form with a multidimensional arrays and I’m trying to send an e-mail with the arrays using PHPmailer by writing the contents of the arrays to a text file and then having it read the file for contents but when I send the e-mail it only shows up as “‘; } fclose(); ?> “. What am I doing wrong?
Here is the code of the form with the multidimensional array:
<form id="form" name="form" method="post" action="send_mail_tray.php" onsubmit='return packageValidator()'>
<table width="100%">
<tr>
<td><input type="hidden" name="appetizer[]" value="Appetizer 1" />Appetizer 1</td>
<td style="width: 75px" align="right">
Med: <input type="text" name="appetizer[0][Med]" style="width: 30px" /><br />
Lg: <input type="text" name="appetizer[0][Lg]" style="width: 30px" />
</td>
<td><input type="hidden" name="appetizer[]" value="Appetizer 2" />Appetizer 2</td>
<td style="width: 75px" align="right">
Med: <input type="text" name="appetizer[1][Med]" style="width: 30px" /><br />
Lg: <input type="text" name="appetizer[1][Lg]" style="width: 30px" />
</td>
<td><input type="hidden" name="appetizer[]" value="Appetizer 3" />Appetizer 3</td>
<td style="width: 75px" align="right">
Med: <input type="text" name="appetizer[2][Med]" style="width: 30px" /><br />
Lg: <input type="text" name="appetizer[2][Lg]" style="width: 30px" />
</td>
</tr>
...
</table>
<input type="Submit" value="Submit Order" />
</form>
My PHP code:
foreach ($_POST['appetizer'] as $piece => $sizes){
$medSize = $sizes['Med'];
$lgSize = $sizes['Lg'];
if(!empty($medSize) || !empty($lgSize)){
$appetizer .= $_GET['appetizer'] . " - Med: " . $medSize . ", Lg: " . $lgSize;
}
}
$message = ("<p>Thank you for your catering request!</p>
<p><b>Name:</b> $name<br />
<b>Phone #:</b> $phone<br />
<b>Email:</b> $email</p>
<p><b>Appetizers:</b><br />
$appetizer
</p>");
I have also tried using andrewsi’s code but after using it, I no longer receive e-mails:
foreach ($_POST['appetizer'] as $piece => $sizes){
$medSize = $sizes['Med'];
$lgSize = $sizes['Lg'];
if(!empty($medSize) || !empty($lgSize)){
$message .= $_GET['appetizer'] . " - Med: " . $medSize . ", Lg: " . $lgSize;
}
}
Here is the code for sending the e-mail:
$email = $_REQUEST['email'];
$mail = new PHPMailer();
$mail->Host = //SMTP server
$mail->Sender = //Sender
$mail->From = //From
$mail->AddReplyTo("email@email.com");
$mail->FromName = //FromName;
$mail->AddAddress($email);
$mail->IsHTML(true);
$mail->Subject = //Subject;
$mail->Body=$message;
$mail->WordWrap = 50;
$mail->Send();
echo"<script>window.location='thankyou.html'</script>";
You need to rejig the last piece of code:
You’re mixing code in with your email text – this way, you’re separating them out.
Edited to add:
You can also generate
$messagestraight from$_POST:That will save you writing and then reading a file.