HTML表单- PHP电子邮件与自定义消息和复选框


HTML Form - PHP Email with custom messages and tickbox

我有一个带有单选按钮、选项框(下拉框)、复选框、文本字段等的HTML表单。我让表单指向另一个文件send。php来发送邮件但我该如何使用复选框和单选按钮以及每个答案之间的输入文本来做到这一点呢?我想把它格式化成这样:

欢迎:{名称}您的年龄组别介于:{带有年龄组别的单选按钮}

以此类推。因为它是私有的,所以我不能给你实际的代码但我可以给你这个代码和格式:

<form action="send.php">
<input type="radio" name="AgeGroup" value="AgeGroup1"> 0-18<br>
<input type="radio" name="AgeGroup" value="AgeGroup2"> 19-29<br>
<input type="radio" name="period" value="AgeGroup3"> 30-39<br>
<input type="radio" name="period" value="AgeGroup4"> 40-49<br>
<input type="radio" name="period" value="AgeGroup5"> 50+<br>
<br><br><br><br>
<select name="Country">
  <option value ="UK">United Kingdom</option>
  <option value ="USA">United States</option>
</option></select>
<br><br><br><br>
<input type="text" name="PostCode" size="5">
<br><br><br><br>
<input type="text" name="HouseNumber" size="5">
<br><br><br><br>
<textarea id="Family" class="input" name="FamilyNumber" rows="10" cols="60"></textarea>
<br><br><br><br>
<input type="checkbox" name="Delievery" value="NextDay"> Next Day Delievery
<br>
<input type="checkbox" name="Delievery" value="TwoToFive"> 2-5 Day
<br>
<input type="checkbox" name="Outcome" value="Dismissed"> Dismissed
<br><br><br><br><br><br>
<center><button id="Send" type="submit" style="height:25px; width:100px; background-color: grey">Send</button></center>
</form>

对不起,太随意了。我没主意了!也很抱歉我的编码能力,我通常不做HTML!

谢谢。

我希望这将有助于…如果我对你的问题理解正确的话。

确保在表单标签中添加一个方法。例如:<form action="send.php" method="post">。在send.php中,您希望通过名称属性获取变量,例如:

$name = $_POST['Name'];
$ageGroup = $_POST['AgeGroup'];

然后你想建立你的电子邮件。PHP允许在双引号字符串中解析变量,这可以帮助您以您想要的方式构建消息。(引用)

$to = "person@example.com";
$subject = "Subject goes here";
$message = "Welcome: $name. Your age group is between: $ageGroup";
//note: headers are optional
$headers = "From: you@example.com" . "'r'n" . "CC: anotherperson@example.com";
mail($to, $subject, $message, $headers);

这只是一个简单的例子,但这可能会让你开始。

您需要为标签添加method="POST"属性。

然后在send.php中读取值,例如PostCode或HouseNumber:

echo "Post code: ".$_POST["PostCode"]."<br />";
echo "House number: "$_POST["HouseNumber"]";
年龄:

if(isset($_POST["AgeGroup"]) {
    echo "Age group: ".$_POST["AgeGroup"];
}
if(isset($_POST["period"]) {
    echo "Age group: ".$_POST["period"];
}

更多信息请访问手册:http://php.net/manual/en/reserved.variables.post.php

这样你就可以把单选按钮组合成一个名字,你可以在php中得到$_POST['name']

的值

<form action="send.php" method="post">
<input type="radio" name="AgeGroup" value="0-18"> 0-18<br>
<input type="radio" name="AgeGroup" value="19-29"> 19-29<br>
<input type="radio" name="AgeGroup" value="30-39"> 30-39<br>
<input type="radio" name="AgeGroup" value="40-49"> 40-49<br>
<input type="radio" name="AgeGroup" value="50+"> 50+<br>
<select name="Country">
  <option value ="UK">United Kingdom</option>
  <option value ="USA">United States</option>
</option></select>
<input type="text" name="PostCode" size="5">
<input type="text" name="HouseNumber" size="5">
<textarea id="Family" class="input" name="FamilyNumber" rows="10" cols="60"></textarea>
<input type="checkbox" name="Delievery" value="NextDay"> Next Day Delievery
<br>
<input type="checkbox" name="Delievery" value="TwoToFive"> 2-5 Day
<br>
<input type="checkbox" name="Outcome" value="Dismissed"> Dismissed
<center><button id="Send" type="submit" style="height:25px; width:100px; background-color: grey">Send</button></center>
</form>