我希望有人可以帮助我。我创建了一个网页,其中包含HTML5和php中的联系表单。当我按下提交按钮时,会将电子邮件发送到应该的位置,但所有字段都是空白的。
我知道我错过了一些简单的事情,但我却看不到它。
这是HTML
<form method="POST" action="submit.php">
<label form="HTML Form">Full Name:</label>
<input maxlength="45" size="35" placeholder="Full Name" required name="name" id="name" type="text">
<label form="HTML Form">Phone Number:</label>
<input maxlength="45" size="35" placeholder="Phone" required name="phone" id="phone" type="text">
<label form="HTML Form">Email Address:</label>
<input maxlength="75" size="35" placeholder="Email Address" required name="email" id="email" type="email">
<label form="HTML Form">Comment:</label>
<textarea maxlength="300" placeholder="Comment"
wrap="soft"
rows="10"
cols="40"
name="comment" id="comment"
required></textarea>
<input formmethod="post" value="Send" formaction="submit.php" type="submit">
<br>
</form>
这里是php
<?php
/* checking if data was sent */
if(isset($_POST))
{
foreach($_POST as $key => $field)
{
if(trim($field==""))
echo "this field is required, your input is blank: <b>$key</b> <br>";
$key=$field; //putting the response into variables
}
/* starting the email message */
$to = "email@mywebsite.com"; // your email address
$subject = "Contact form submission";
$message = "Full Name: $name <br> Phone: $phone <br>";
$message .= "Email Address: $email <br> Comment: <br> $comment";
$headers = "From:" . $from."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
if(mail($to,$subject,$message,$headers))
{
echo "Email was sent";
}
}
?>
答案 0 :(得分:1)
错误在于$_POST
变量的分配:
foreach($_POST as $key => $field)
{
if(trim($field==""))
echo "this field is required, your input is blank: <b>$key</b> <br>";
$$key=$field; //putting the response into variables # <------
}
您必须使用语法$$key
创建variable variable。
您的作业将$key
变量设置为$_POST
数组的最后一个值。
另请注意:在哪里定义$from
变量?
答案 1 :(得分:0)
您没有从POST数组中获取变量。 例如 $ phone = $ _POST [&#39;电话&#39;]等..
答案 2 :(得分:0)
如果您尝试将$_POST
键名称转换为变量,则需要执行此操作:$$key = $field;
而不是$key = $field;
。这些是variable variables。