如果您为动态创建的字段(以及相同类型的预渲染版本)提供一个name
属性,delegates[]
那么 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) {
...
}
请参阅如何定义表单字段数组 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.");
}
?>