有没有更实用的方式来发送一个非常大的表单在php


is there a more practical way to send a very big form in php?

客户在他的网站上有一个非常大的表单(大约40个字段),一旦用户点击提交,就应该发送到他的邮件。

我想这样做:

<?
$name = $_REQUEST['name'] ;
$lastname = $_REQUEST['lastname'] ;
$address = $_REQUEST['lastname'] ;
$field1 = $_REQUEST['field1'] ;
$field2 = $_REQUEST['field2'] ;
$field39 = $_REQUEST['field39'] ;
....

$headers  = 'MIME-Version: 1.0' . "'r'n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
// Additional headers
$headers .= 'To: Bob <client@example.com>' . "'r'n";
$headers .= 'From: User <example@user.com>' . "'r'n";
// Mail it
mail("client@example.com", "FORM FROM YOUR WEB!", "Name: $name'r'n LastName: $lastname'r'n Address: $address'r'n       
Answer to field 1: $field1 ..... ", $headers);
?>

但是由于大约有40个字段,我不确定我的方法是否正确。有没有更合适的方法来做这件事?

这在使用range:

的循环中并不难做到。
foreach(range(1, 39) as $number) {
    $message .= "Answer to field $number: " . $_REQUEST['field' . $number] . "'n";
}

不用担心,这是一种非常常见的方式。最大邮件大小取决于供应商,但现在每个人都应该有10mb以上。

一个更详细的解决方案是保存一个excel表或pdf文件(是的,php可以做到这一点),并通过邮件发送-但这是更多的工作。

(顺便说一句,你可以循环你的字段,以节省40次重复,例如foreach ($fields as $field) {...})

您可以循环遍历所有表单字段并创建消息字符串。

类似:

function contains($string, $subString)
{
  if (strpos($string, $subString) === false){
    return false;
  }
  return true;
}

foreach ($_REQUEST as $key => $value)
{
   if (!contains($key, "field")) continue;
   $msg .= $key;
   $msg .= $value;
}
//now content of your message with form is in $msg

如果您使用foreach,您可以以不同的方式命名您的字段。然后,您将继续的条件反转并放入不应该处理的字段