I am using a PHP script to generate an email based on the information from a form. The form has a variable number of rows.
I have converted the names of the inputs in each row in the form to an array, by adding []
after the name, so that the data in all of the rows is available for generating the email.
However, what I don't know how to do is how to construct the PHP so that it can generate an email with just the right number of rows in the email.
At the moment I have just set the PHP to read the first 5 items in the array for each input, and construct the body of the email using these. The problem with this approach is that if the user adds more than 5 rows data will be lost, and if there is less than 5 rows, there will be unnecessary text in the email for 'name, email, telephone'.
I wonder if there is a way of getting the PHP to read the array for any number of rows, and generate an email with just the correct number of rows? I have included the PHP as it stands below.
Thanks,
Nick
<?php
$EmailFrom = "";
$EmailTo = "";
$Subject = "";
$Name = Trim(stripslashes($_POST['name'][0]));
$Email = Trim(stripslashes($_POST['email'][0]));
$Telephone = Trim(stripslashes($_POST['telephone'][0]));
$Name2 = Trim(stripslashes($_POST['name'][1]));
$Email2 = Trim(stripslashes($_POST['email'][1]));
$Telephone2 = Trim(stripslashes($_POST['telephone'][1]));
$Name3 = Trim(stripslashes($_POST['name'][1]));
$Email3 = Trim(stripslashes($_POST['email'][1]));
$Telephone3 = Trim(stripslashes($_POST['telephone'][2]));
$Name4 = Trim(stripslashes($_POST['name'][1]));
$Email4 = Trim(stripslashes($_POST['email'][1]));
$Telephone4 = Trim(stripslashes($_POST['telephone'][3]));
$Name5 = Trim(stripslashes($_POST['name'][开发者_运维百科1]));
$Email5 = Trim(stripslashes($_POST['email'][1]));
$Telephone5 = Trim(stripslashes($_POST['telephone'][4]));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "New bookings have been made for the Ajahn Amaro Retreat as follows:";
$Body .= "\n";
$Body .= "\n";
$Body .= "name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "\n";
$Body .= "email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "\n";
$Body .= "telephone: ";
$Body .= $Telephone;
$Body .= "\n";
$Body .= "\n";
$Body .= "\n";
$Body .= "name: ";
$Body .= $Name2;
$Body .= "\n";
$Body .= "\n";
$Body .= "email: ";
$Body .= $Email2;
$Body .= "\n";
$Body .= "\n";
$Body .= "telephone: ";
$Body .= $Telephone2;
$Body .= "\n";
$Body .= "\n";
$Body .= "\n";
$Body .= "name: ";
$Body .= $Name3;
$Body .= "\n";
$Body .= "\n";
$Body .= "email: ";
$Body .= $Email3;
$Body .= "\n";
$Body .= "\n";
$Body .= "telephone: ";
$Body .= $Telephone3;
$Body .= "\n";
$Body .= "\n";
$Body .= "\n";
$Body .= "name: ";
$Body .= $Name4;
$Body .= "\n";
$Body .= "\n";
$Body .= "email: ";
$Body .= $Email4;
$Body .= "\n";
$Body .= "\n";
$Body .= "telephone: ";
$Body .= $Telephone4;
$Body .= "\n";
$Body .= "\n";
$Body .= "\n";
$Body .= "name: ";
$Body .= $Name5;
$Body .= "\n";
$Body .= "\n";
$Body .= "email: ";
$Body .= $Email5;
$Body .= "\n";
$Body .= "\n";
$Body .= "telephone: ";
$Body .= $Telephone5;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=payment.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
Do something like this, iterating over one of the arrays of data:
foreach($_POST['name'] as $i => $name){
echo $name;
echo $_POST['email'][$i];
echo $_POST['telephone'][$i];
}
Except instead of printing the data, add it to the string that will be your email's body.
Instead of assigning each one to a unique variable, just put them in an array.
$body = '';
$row_count = count($_POST['name']);
for($i = 0; $i < $row_count; $i++)
{
// variable sanitation...
$name = trim(stripslashes($_POST['name'][$i]));
$email = trim(stripslashes($_POST['email'][$i]));
$telephone = trim(stripslashes($_POST['telephone'][$i]));
// this assumes name, email, and telephone are required & present in each element
// otherwise you will have spurious line breaks.
$body .= $name . "\n\n" . $email . "\n\n" . $telephone . "\n\n";
}
// send email
$success = mail($emailTo, $subject, $body, "From: <$EmailFrom>");
Also on a purely stylistic note, your variables should begin with lower-case letters.
精彩评论