好的,所以我有一個聯系表格,我希望人們選擇多個專案,一旦他們提交聯系表格,我希望它使用 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 = "[email protected]"; // Specify the email address you want to send the mail to.
$email_from = "[email protected]"; //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>
uj5u.com熱心網友回復:
你需要內爆
代替
foreach($products as $product){
$list = $product . "<br> test <br>";
$html = str_replace("{{list}}",$list,$html);
};
和
$list = implode("<br>",$products);
$html = str_replace("{{list}}",$list,$html);
uj5u.com熱心網友回復:
當你這樣做時:
foreach($products as $product){
$list = $product . "<br> test <br>";
$html = str_replace("{{list}}",$list,$html);
};
您實際上進行了 n 次替換,但只有第一個會起作用,因為之后沒有更多的“{{list}}”要替換。這不是錯誤,只是不是您所期望的。
像這樣嘗試:
$html = str_replace("{{message}}",$message,$html);
$list = '';
foreach($products as $product){
$list .= $product . "<br>";
};
$html = str_replace("{{list}}",$list,$html);
現在您正在使用所有選定的產品構建一個臨時字串變數,然后您只需替換它一次。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/365830.html
