【发布时间】:2022-01-03 14:26:42
【问题描述】:
好的,所以我有一个联系表单,我希望人们选择多个项目,一旦他们提交联系表单,我希望它使用 html 模板发送电子邮件。我已将其设置为字符串替换 html 模板中的数据,但每次我尝试执行数组时,它都会说数组或仅显示已选择的多个项目之一。
这是我确保添加 [] 以将名称放入数组的 HTML 选择代码。
<select class="js-example-multiple js-states form-control" multiple="multiple" name="product[]">
<option value="Baby Shark Castle 15ft x 18ft">Baby Shark Castle 15ft x 18ft</option>
<option value="Pirate's assault course 12ft x 25ft">Pirate's assault course 12ft x 25ft</option>
<option value="Yellow Mega Slide 18ftx18ft">Yellow Mega Slide 18ftx18ft</option>
<option value="18ft x 18ft Disco Dome Lights & Speaker">18ft x 18ft Disco Dome Lights & Speaker</option>
<option value="Assault Course 35ft Long 12 ft Wide">Assault Course 35ft Long 12 ft Wide</option>
<option value="Inflatable Nightclub 12ft x 15ft">Inflatable Nightclub 12ft x 15ft</option>
<option value="40ft Assault course 15ft x 40ft">40ft Assault course 15ft x 40ft</option>
<option value="Inflatable Pub 17x17 - Holds 20 People">Inflatable Pub 17x17 - Holds 20 People</option>
</select>
这是 php 代码,我已经成功替换了其他单个值,但是当我尝试用多个值替换一个时,它只显示一个值。我尝试了一个 foreach 循环,但这仅在我回显 $product 值时才有效。我想将所有选定的项目串起来,而不仅仅是一个,我有一个我希望它看起来像下面的示例。
// Bring in the email template here
$html = file_get_contents('template.html');
// You only need to modify the following three lines of code to customise your form to mail script.
$email_to = "aaron@pugmanmedia.co.uk"; // Specify the email address you want to send the mail to.
$email_from = "info@pugmanmedia.co.uk"; //Specify the email address that you want to send the email from. This needs to be Fasthosts hosted,
$email_subject = "Website Contact Form"; // Set the subject of your email.
// Specify a page on your website to display a thankyou message when the mail is sent
$thankyou_url = "../thankyou.html";
// Get the details the user entered into the form
$name = $_POST["name"];
$reply_to = $_POST["email"];
$number = $_POST["number"];
$date = $_POST["date"];
$message = $_POST["message"];
$products = $_POST["product"];
// Validate the email address entered by the user
if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
// Invalid email address
die("The email address entered is invalid.");
}
// Replacing the details in the template the above variables
$html = str_replace("{{username}}",$name,$html);
$html = str_replace("{{email}}",$reply_to,$html);
$html = str_replace("{{number}}",$number,$html);
$html = str_replace("{{date}}",$date,$html);
$html = str_replace("{{message}}",$message,$html);
foreach($products as $product){
$list = $product . "<br> test <br>";
$html = str_replace("{{list}}",$list,$html);
};
这是html模板代码
<div style="margin: 0px; padding: 0px;">
<p style="margin: 0px; padding: 0px;">
{{list}}
</p>
</div>
这是我得到的结果
<div style="margin: 0px; padding: 0px;">
<p style="margin: 0px; padding: 0px;">
Pirate's assault course 12ft x 25ft
</p>
</div>
我想要的最终结果是当人们选择任意数量的项目使其全部出现在这样的一个地方而不是上面
<div style="margin: 0px; padding: 0px;">
<p style="margin: 0px; padding: 0px;">
Baby Shark Castle 15ft x 18ft
Assault Course 35ft Long 12 ft Wide
Pirate's assault course 12ft x 25ft
and so on
</p>
</div>
【问题讨论】:
标签: php html arrays email str-replace