我创建了一个html表单,旨在执行两项操作 - 将表单信息提交到指定的电子邮件地址,并将用户重定向到网站上的另一个页面。我创建了一个.php表单来管理这些操作。表单成功重定向并发送电子邮件。但是,电子邮件具有正确的目的地,主题和消息,但无法提取用户提交给表单的信息 - 即姓名,地址,电子邮件等。
我对网络开发并不陌生,但我很欣赏您能提供的任何见解。谢谢!
HTML:
<section>
<div class="row">
<div class="col-lg-offset-3 col-lg-6 col-md-12"><form name="Whitepaper
Request form" method="POST" action="Info-Request.php" class="text-center">
<input name="First_Name" type="text" required class="contact-field-sm"
id="fname" placeholder="First Name">
<input name="Last_Name" type="text" required class="contact-field-sm"
id="lname" placeholder="Last Name"><br><br>
<input name="Address" type="text" class="contact-field-sm" id="address"
placeholder="Address">
<input name="City" type="text" class="contact-field-sm" id="city"
placeholder="City"><br><br>
<input name="State" type="text" class="contact-field-sm" id="state"
placeholder="State">
<input name="Zip" type="text" class="contact-field-sm" id="zip"
placeholder="Zip"><br><br>
<input name="Phone" type="tel" class="contact-field-sm" id="phone"
placeholder="Phone">
<input name="Email" type="email" class="contact-field-sm" id="email"
placeholder="Email"><br><br>
<input name="Company" type="text" required class="contact-field-sm"
id="company" placeholder="Company">
<input name="Title" type="text" required class="contact-field-sm" id="title"
placeholder="Title"><br><br>
<input id="submit" type="submit" value="Submit">
</form>
PHP:
<?php
if(!isset($_POST['submit']))
$to = "email@example.com";
$from = 'Whitepaper Request Form';
$email_subject = " ";
$email_message = "Results from Whitepaper Request Form:\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$First_Name = $_POST['First_Name'];
$Last_Name = $_POST['Last_Name'];
$email_message .= "First Name: ".clean_string($First_Name)."\n";
$email_message .= "Last Name: ".clean_string($Last_Name)."\n";
$email_message .= "Address: ".clean_string($Address)."\n";
$email_message .= "City: ".clean_string($City)."\n";
$email_message .= "State: ".clean_string($State)."\n";
$email_message .= "Zip: ".clean_string($Zip)."\n";
$email_message .= "Phone: ".clean_string($Phone)."\n";
$email_message .= "Email: ".clean_string($Email)."\n";
$email_message .= "Company: ".clean_string($Company)."\n";
$email_message .= "Title: ".clean_string($Title)."\n";
mail($to,$from,$email_subject,$email_message);
header('Location: Example_Retro_WhtP.pdf');
?>
[编辑] PHP:
<?php
if(isset($_POST['submit'])){
$to = 'example@example.com';
$subject = 'Whitepaper Request Form';
$email_message = 'Results from Whitepaper Request Form:\n\n';
$headers = 'From: example@example.com';
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($_POST['First_Name'])."\n";
$email_message .= "Last Name: ".clean_string($Last_Name)."\n";
$email_message .= "Address: ".clean_string($Address)."\n";
$email_message .= "City: ".clean_string($City)."\n";
$email_message .= "State: ".clean_string($State)."\n";
$email_message .= "Zip: ".clean_string($Zip)."\n";
$email_message .= "Phone: ".clean_string($Phone)."\n";
$email_message .= "Email: ".clean_string($Email)."\n";
$email_message .= "Company: ".clean_string($Company)."\n";
$email_message .= "Title: ".clean_string($Title)."\n";
mail($to,$subject,$email_message,$headers);
header('Location: Example_Retro_WhtP.pdf');
exit;
}
?>
答案 0 :(得分:1)
(主要)问题在于下面使用的参数,还有一些其他问题。
mail($to,$from,$email_subject,$email_message);
PHP mail()
的语法是这样,按以下顺序:
根据手册的语法:
bool mail(string $ to,string $ subject,string $ message [,string $ additional_headers [,string $ additional_parameters]])
遵循该语法,邮件将包含用于发送的所有内容。
注意:您需要添加其他输入缺少的POST数组。
但是,您声明电子邮件已发送。在看到以下情况时:
if(!isset($_POST['submit']))
这就是你的代码发送邮件的原因,因为没有提交输入带有该名称,因此它永远不会“设置”。
<input id="submit" type="submit" value="Submit">
读作:
<input id="submit" type="submit" name="submit" value="Submit">
并使用适当的支撑技术。
即:注意,我删除了!
运算符,这意味着“不是”:
if(isset($_POST['submit'])){
// run your code in here to be executed
}
添加else{...}
条件。
还要检查PHP错误报告中的错误:
在标题之后添加exit;
也不会受到影响,甚至建议阻止进一步执行(如果你下面有更多的代码)。
最好使用empty()
和isset()
检查空字段。