【问题标题】:Mailing a form with dynamic number of entries邮寄具有动态条目数的表单
【发布时间】:2013-11-04 14:12:33
【问题描述】:

我有一个表单,可以根据用户提供的数字动态添加输入。该表单用于预订课程,用户选择参加人数,表单添加输入以读取他们的详细信息。

只需将 FOR 循环的计数插入相关属性即可生成和命名输入:

var inputEl = $('<input type="text" class="text delegate" name="delegate_name_' + i + '" id="delegate-name-' + i + '" placeholder="Delegate Name ' + (i + 1) + '" /><input type="text" class="text delegate" name="delegate_email_' + i + '" id="delegate-email-' + i + '" placeholder="Delegate Email ' + (i + 1) + '" /><input type="text" class="text delegate" name="delegate_tel_' + i + '" id="delegate-tel-' + i + '" placeholder="Delegate Telephone ' + (i + 1) + '" />')

这一切都很好,花花公子,工作正常。但是,我正要编写用于邮寄表单的 PHP,我突然想到我不知道如何告诉邮件脚本它需要读取多少输入。

我认为它需要另一个 FOR 循环来运行并创建相关的电子邮件条目,但我对 PHP 的了解有限。有什么想法吗?

【问题讨论】:

  • 我会使用 name="delegate_name[]" 而不是 delegate_name_(increment) ,这将创建一个代表名称数组

标签: php forms email


【解决方案1】:

如果您为动态创建的字段(以及相同类型的预渲染版本)提供delegates[]name 属性,那么PHP 会将其作为数组在内部存储。

<input type="text" name="delegates[]" id="delegate1">
<input type="text" name="delegates[]" id="delegate2">
<input type="text" name="delegates[]" id="delegate3">
<input type="text" name="delegates[]" id="delegate4">

然后应该很容易迭代该数组,执行您需要的操作。

foreach ($_POST['delegates'] as $delegate) {
    ...
}

How to define an array of form fields PHP

特别是对于您发布的代码,我相信您需要做类似的事情:

<?php
    // Set properties
    $to     = "mail@mail.com";  // Enter the email address to send email to.
    $subject    = "Booking Form Submission";            // Enter a subject for your email.

    // Set the web address to forward the user to when the mail is successfully sent.
    $url        = "success.html";

    $message = $_POST['message'];

    foreach ($_POST['delegates'] as $delegate) {
        $message .= "\r\n$delegate";
    }

    // Send the email, you don't need to change anything below this line.
    $sent = mail($to, $subject, $message, "From: " . $_POST["email"], "-f" . $_POST["email"], "Telephone:" . $_POST["tel"], "Payment Method:". $_POST["payment"], "Payment Address:" . $_POST["address"], "Purchase Order Number:" . $_POST["pono"], "No. of Delegates:" . $_POST["delno"], "Residential?:" . $_POST["residential"], "Exam?:" . $_POST["exam"], "Total Cost:" . $_POST["total_cost"]);

    // See if mail was sent
    if($sent) {
        // Email was sent successfully. Note that all we can see is that the mail was sent
        // from the server, but we cannot determine if it arrived at it's destination.
        header("Location: " . $url);
    } else {
        // Mail was not sent
        die("Your email has not been sent.");
    }
?>

【讨论】:

  • 谢谢,我试试看。
  • 你能告诉我如何将它集成到邮件中,这里:pastebin.com/ywPvPM5Y
  • 我不知道你想做什么。很简单,在循环内部,对于每次迭代,您都将获得表单中的代表详细信息。如果您要发送单个电子邮件,请在循环内完成所有操作。如果您要向多个人发送一封电子邮件,那么您可能需要连接一个字符串以插入到 to、cc 或 bcc 字段中 - 在这种情况下,循环内唯一的内容就是连接。
  • 我只是想把表格发给一个人
  • 您需要说明您希望如何处理这些数据。我们有一个包含委托详细信息的数组 - 我们需要对这些数据做一些事情,但我不知道你想用它做什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-21
  • 1970-01-01
  • 2019-11-24
  • 2015-01-04
  • 1970-01-01
  • 2016-05-12
  • 1970-01-01
相关资源
最近更新 更多