在具有多個復選框以選擇產品的頁面上,我想發送一封帶有所選復選框的電子郵件。
以下代碼在點擊提交按鈕后成功輸出了我喜歡的內容,但是在網頁上。我只是不知道如何將結果包含在電子郵件中。
這是html表單:
<form action="" method="post">
<label for="product1"><input type="checkbox" class="equipment[]" id="product1" name="product1" value="100">Add to inquiry</label>
<label for="product2"><input type="checkbox" class="equipment[]" id="product2" name="product2" value="50">Add to inquiry</label>
<label for="product3"><input type="checkbox" class="equipment[]" id="product3" name="product3" value="80">Add to inquiry</label>
<input type="submit" name="submit" class="formsubmitbtn" value="Send inquiry">
</form>
這是在頁面上成功列印結果的 PHP 代碼:
<?php
if(isset($_POST['product1'])){
echo "checked Product Name 1"."<br>";
}
if(isset($_POST['product2'])){
echo "checked Product Name 2"."<br>";
}
if(isset($_POST['product3'])){
echo "checked Product Name 3";
}
?>
結果(對我來說是完美的)是:
選中產品名稱 1
選中產品名稱 2
選中產品名稱 3
現在我想把結果放在電子郵件中。這是電子郵件的 PHP:
<?php
if(isset($_POST['submit'])){
$to = "myemailaddress";
$from = $_POST['email']; // this is the sender's Email address
$gearselection = ???;
$subject = "Inquiry";
$message = $from . "\n\n" . $gearselection;
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Your inquiry has been sent.";
}
?>
在$gearselection我想從上面的代碼中輸出結果。
任何人都可以幫忙嗎?
uj5u.com熱心網友回復:
首先,它應該是name獲取方括號的屬性,而不是類,并且該name屬性應該在input復選框上定義,而不是label.
<form action="" method="post">
<input type="checkbox" name="product[]" id="product1" value="Product 1">
<label for="product1">Product 1</label>
<input type="checkbox" name="product[]" id="product2" value="Product 2">
<label for="product2">Product 2</label>
<input type="checkbox" name="product[]" id="product3" value="Product 3">
<label for="product3">Product 3</label>
<input type="submit" name="submit" class="formsubmitbtn" value="Send inquiry">
</form>
然后在 PHP 端,您將獲得一組值,具體取決于檢查的內容$_POST['product']- 您可以遍歷它們以獲取所有被選中的專案。
if(!empty($_POST['product'])) {
foreach($_POST['product'] as $p) {
echo $p;
}
}
在您的電子郵件中,您可以執行以下操作:
if(!empty($_POST['product'])) {
$gearselection = implode(', ', $_POST['product']);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/426622.html
上一篇:標簽的樣式不回應CSS
下一篇:在角度的ngClass中使用form.controls.errors.(myError)(錯誤來自AbstractControl)時遇到問題
