生成的电子邮件转换PHP脚本与表单变量行工作 [英] Converting PHP script for generating email to work with variable rows in form

查看:380
本文介绍了生成的电子邮件转换PHP脚本与表单变量行工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP脚本的基础上从表单中的信息来生成电子邮件。形式具有可变的行数。

我在表格每一行转换输入的名称到一个数组中,加入 [] 名称后,使所有的数据行可用于生成的电子邮件。

不过,我不知道该怎么做的是如何构建PHP,以便它可以生成在电子邮件行恰到好处的号码的电子邮件。

目前,我刚才设置PHP读取阵列的每个输入中的第一个5个项目,并利用这些构造电子邮件的正文。这种方法的问题是,如果用户添加多于5行的数据将丢失,如果有小于5行,将有在电子邮件中关于'姓名,电子邮件,电话不必要的文本。

我不知道是否有获得PHP只用正确的行数来读取任意行数的数组,并生成电子邮件的方式?我已经包括PHP,因为它站在下面。

谢谢,

尼克

 < PHP$ EmailFrom =;
$ EmailTo =;
$主题=;
$名称=修剪(的stripslashes($ _ POST ['名'] [0]));
$电子邮件=修剪(的stripslashes($ _ POST [电子邮件] [0]));
$电话=修剪(的stripslashes($ _ POST [电话] [0]));
$名称2 =修剪(的stripslashes($ _ POST ['名'] [1]));
$电子邮件2 =修剪(的stripslashes($ _ POST [电子邮件] [1]));
$ Telephone2 =修剪(的stripslashes($ _ POST [电话] [1]));
$ NAME3 =修剪(的stripslashes($ _ POST ['名'] [1]));
$电子邮件3 =修剪(的stripslashes($ _ POST [电子邮件] [1]));
$ Telephone3 =修剪(的stripslashes($ _ POST [电话] [2]));
$ NAME4 =修剪(的stripslashes($ _ POST ['名'] [1]));
$ EMAIL4 =修剪(的stripslashes($ _ POST [电子邮件] [1]));
$ Telephone4 =修剪(的stripslashes($ _ POST [电话] [3]));
$ NAME5 =修剪(的stripslashes($ _ POST ['名'] [1]));
$ Email5 =修剪(的stripslashes($ _ POST [电子邮件] [1]));
$ Telephone5 =修剪(的stripslashes($ _ POST [电话] [4]));//验证
$ validationOK =真;
如果(!$ validationOK){
  打印< META HTTP-当量= \\刷新\\的内容= \\0; URL = error.htm \\>中;
  出口;
}// prepare电子邮件正文
$身体=新订单已作出了阿姜阿马罗撤退如下:
$身体=\\ n。
$身体=\\ n。
$身体=名称:。
$身体= $名称。
$身体=\\ n。
$身体=\\ n。
$身体=电子邮件。
$身体= $电子邮件。
$身体=\\ n。
$身体=\\ n。
$身体=电话:。
$身体= $电话。
$身体=\\ n。
$身体=\\ n。
$身体=\\ n。
$身体=名称:。
$身体= $名称2。
$身体=\\ n。
$身体=\\ n。
$身体=电子邮件。
$身体= $电子邮件2。
$身体=\\ n。
$身体=\\ n。
$身体=电话:。
$身体= $ Telephone2。
$身体=\\ n。
$身体=\\ n。
$身体=\\ n。
$身体=名称:。
$身体= $ NAME3。
$身体=\\ n。
$身体=\\ n。
$身体=电子邮件。
$身体= $电子邮件3。
$身体=\\ n。
$身体=\\ n。
$身体=电话:。
$身体= $ Telephone3。
$身体=\\ n。
$身体=\\ n。
$身体=\\ n。
$身体=名称:。
$身体= $ NAME4。
$身体=\\ n。
$身体=\\ n。
$身体=电子邮件。
$身体= $ EMAIL4。
$身体=\\ n。
$身体=\\ n。
$身体=电话:。
$身体= $ Telephone4。
$身体=\\ n。
$身体=\\ n。
$身体=\\ n。
$身体=名称:。
$身体= $ NAME5。
$身体=\\ n。
$身体=\\ n。
$身体=电子邮件。
$身体= $ Email5。
$身体=\\ n。
$身体=\\ n。
$身体=电话:。
$身体= $ Telephone5。
$身体=\\ n。// 发电子邮件
$成功=电子邮件($ EmailTo,$主题,$机构来源:< $ EmailFrom>);
//重定向到成功页面
如果($成功){
  打印< META HTTP-当量= \\刷新\\的内容= \\0; URL = payment.html \\>中;
}
其他{
  打印< META HTTP-当量= \\刷新\\的内容= \\0; URL = error.htm \\>中;
}
?>


解决方案

而不是指定每一个到了独特的可变的,只是把它们放入数组。

  $身体='';
$ ROW_COUNT =计数($ _ POST ['名']);为($ I = 0; $ I< $ ROW_COUNT; $ I ++)
{
  //变量卫生...
  $名=修剪(的stripslashes($ _ POST ['名'] [$ i]));
  $电子邮件= TRIM(的stripslashes($ _ POST [电子邮件] [$ i]));
  $电话= TRIM(的stripslashes($ _ POST [电话] [$ i]));  //这个假设需要与放大器的姓名,电子邮件和电话;中每个元素present
  //否则就会产生虚假的换行符。
  $体。= $名称。 \\ n \\ n。 $电子邮件。 \\ n \\ n。 $电话。 \\ n \\ n;
}// 发电子邮件
$成功=电子邮件($ emailTo,$主题,$机构来源:< $ EmailFrom>);

另外一个纯粹的文体记,你的变量应该以小写字母开头。

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

解决方案

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.

这篇关于生成的电子邮件转换PHP脚本与表单变量行工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆