转换PHP脚本以生成电子邮件以使用表单中的变量行

时间:2011-05-15 16:45:43

标签: php arrays email forms variables

我正在使用PHP脚本根据表单中的信息生成电子邮件。表单的行数可变。

我已将表单中每行输入的名称转换为数组,方法是在名称后添加[],以便所有行中的数据都可用于生成电子邮件。

然而,我不知道如何构建PHP,以便它可以生成一封电子邮件中包含正确行数的电子邮件。

目前我刚刚设置PHP以读取数组中每个输入的前5个项目,并使用这些项目构建电子邮件的正文。这种方法的问题在于,如果用户添加超过5行,则数据将丢失,如果少于5行,则电子邮件中将出现“名称,电子邮件,电话”中不必要的文本。

我想知道是否有办法让PHP为任意数量的行读取数组,并生成一个只有正确行数的电子邮件?我已经包含了PHP,如下所示。

谢谢,

尼克

<?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\">";
}
?>

2 个答案:

答案 0 :(得分:2)

执行类似这样的操作,迭代其中一个数据数组:

foreach($_POST['name'] as $i => $name){
   echo $name;
   echo $_POST['email'][$i];
   echo $_POST['telephone'][$i];
}

除了打印数据外,不要将其添加到将成为电子邮件正文的字符串中。

答案 1 :(得分:1)

不要将每个变量分配给唯一变量,而只需将它们放入数组中。

$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>");

同样在纯粹的文体上,你的变量应该以小写字母开头。