我是网页设计与开发的学生,试图建立一个网站。我使用php和html创建了一个网站的联系表单。我是php的新手,所以我对此一无所知。我从网站上复制了一个代码,通过该代码我收到一封电子邮件但是空了[没有我在网站联系表格中输入的信息]。 这是我的HTML代码[文件名:contact.html]: -
<div class="login-w3l">
<div class="top-img-agileits-w3layouts">
<h2 class="sub-head-w3-agileits">Contact Us</h2>
<p>Fillout the form below !</p>
<div class="login-form">
<form action="handler.php" method="post" enctype="multipart/form-data">
<input type="text" name="Name" placeholder="First Name" required="" />
<input type="text" name="Name" placeholder="Last Name" required="" />
<input type="email" name="Email" placeholder="Email" required="" />
<input type="text" name="Subject" placeholder="Subject" required="" />
<textarea name="Message" placeholder="Message" required=""></textarea>
<input type="submit" value="Send">
</form>
</div>
</div>
</div>
-----End of HTML-----
这是我的PHP代码[文件名:handler.php]: -
<?php
$first_name = $_POST ['first name'];
$last_name = $_POST ['last name'];
$visitor_email = $_POST ['email'];
$message = $_POST ['message'];
$email_from = 'info@wms-wms-wms.tk';
$email_subject = "WMS Contact form";
$email_body = "User First Name : $first_name \n".
"User Last Name : $last_name \n".
"User Email : $visitor_email \n ".
"User Message : $message \n";
$to = "wms-wms-wms@outlook.sa";
$headers = "From: $email_from \r\n";
$headers .= "Reply To: $visitor_email \r\n";
mail($to, $email_subject, $email_body, $headers);
header("Location: contact.html");
?>
答案 0 :(得分:0)
首先,您需要适当地格式化表单元素。添加唯一id
和<div class="login-w3l">
<div class="top-img-agileits-w3layouts">
<h2 class="sub-head-w3-agileits">Contact Us</h2>
<p>Fillout the form below !</p>
<div class="login-form">
<form action="handler.php" method="post" enctype="multipart/form-data">
<input type="text" name="first_name" id="first_name" placeholder="First Name" required=""/>
<input type="text" name="last_name" id="last_name" placeholder="Last Name" required=""/>
<input type="email" name="email" id="email" placeholder="Email" required=""/>
<input type="text" name="subject" id="subject" placeholder="Subject" required=""/>
<textarea name="message" id="message" placeholder="Message" required=""></textarea>
<input type="submit" value="Send">
</form>
</div>
</div>
</div>
属性与表单提交相关。所以,我更新了你的名字和id元素,以匹配你的php脚本正在寻找的东西。
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
....
?>
之后,您的变量顶部现在应该填充您之前填写的表单元素。
以下是您编辑的实际工作的PHP:
enumerate()
答案 1 :(得分:0)
确保name
和id
属性全部为小写且没有空格。
也不要在$_POST
和[]
之间添加空格。
尝试使用:
<div class="login-w3l">
<div class="top-img-agileits-w3layouts">
<h2 class="sub-head-w3-agileits">Contact Us</h2>
<p>Fillout the form below !</p>
<div class="login-form">
<form action="handler.php" method="post" enctype="multipart/form-data">
<input type="text" name="first_name" id="first_name" placeholder="First Name" required=""/>
<input type="text" name="last_name" id="last_name" placeholder="Last Name" required=""/>
<input type="email" name="email" id="email" placeholder="Email" required=""/>
<input type="text" name="subject" id="subject" placeholder="Subject" required=""/>
<textarea name="message" id="message" placeholder="Message" required=""></textarea>
<input type="submit" value="Send">
</form>
</div>
</div>
</div>
PHP:
<?php
$first_name = $_POST['first name'];
$last_name = $_POST['last name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = 'info@wms-wms-wms.tk';
$email_subject = "WMS Contact form";
$email_body = "User First Name : $first_name \n".
"User Last Name : $last_name \n".
"User Email : $visitor_email \n ".
"User Message : $message \n";
$to = "wms-wms-wms@outlook.sa";
$headers = "From: $email_from \r\n";
$headers .= "Reply To: $visitor_email \r\n";
mail($to, $email_subject, $email_body, $headers);
header("Location: contact.html");
?>