我是网络开发的初学者,我正在建立一个有联系表格的网站,当输入详细信息时,必须将详细信息发送到我的邮件ID。但我不确定为什么代码不起作用。
<?php
if(isset($_POST["submit"])){
// Checking For Blank Fields..
if($_POST["name"]==""||$_POST["email"]==""||$_POST["note"]==""){
echo "Fill All Fields..";
}else{
// Check if the "Sender's Email" input field is filled out
$email=$_POST['email'];
// Sanitize E-mail Address
$email =filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email){
echo "Invalid Sender's Email";
}
else{
$message = $_POST['note'];
$headers = 'From:'. $email2 . "\r\n"; // Sender's Email
$headers .= 'Cc:'. $email2 . "\r\n"; // Carbon copy to Sender
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// Send Mail By PHP Mail Function
mail("[ email removed ]", $message, $headers);
echo "Your mail has been sent successfuly ! Thank you for your feedback";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hire-a-Tent</title>
<meta name="description" content="">
<meta name="author" content="">
<!-- Le styles -->
<link href="css/style.css" rel="stylesheet">
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<![endif]-->
<!-- jQuery -->
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<!-- Javascript -->
<script src="js/jquery.placeholder.js"></script>
<script src="js/custom.js"></script>
<!-- Fav and touch icons -->
<link rel="shortcut icon" href="favicon.ico">
</head>
<body>
<div class="background">
<div class="wrapper">
<header>
<div class="tel"><span>CALL US:</span> 9738479234 <br /><span style="padding-left:150px;">9731015469</span></div>
<div class="social">
<a href="#"><img src="images/facebook.png" alt="" /></a>
<a href="#"><img src="images/twitter.png" alt="" /></a>
</div>
</header>
<div class="sidebar">
<div class="order-form">
<div class="order-form-head">
<h1>Rent -a- Tent</h1>
</div>
<div class="taxi-line"></div>
<!-- Contact Form -->
<form action="email_code.php" id="form" method="post" name="form">
<div class="inp"><input type="text" class="required" name="name" id="name" placeholder="Name..." /></div>
<div class="inp"><input type="text" class="required" name="tel" id="tel" placeholder="Telephone..." /></div>
<div class="inp"><input type="text" class="required" name="email" id="email" placeholder="Email..." /></div>
<div class="inp"><textarea class="required" name="note" id="note" placeholder="Message..."></textarea></div>
<button type="submit"></button>
<div class="spacer"></div>
</form>
<?php include "email_code.php"?>
</div>
<div class="address-box">
<img src="images/icon.png" class="icon" alt="" />
<div class="text">
<span>We are located at</span><br />
85, 4th cross road,<br />
GKW layout, vijayanagar<br />
Bengaluru-40
</div>
</div>
</div>
<div class="character">
<!-- <img src="images/transparent2.png" class="taxi-driver" alt="" /> -->
<marquee behavior="alternate">T2 Tent: Rs 100/day --- T3 Tent: Rs 200/day -- Sleeping Bag: Rs 50/day --</marquee>
</div>
<div class="spacer"></div>
<footer>
<p><strong><br/><br/>Hire-a-tent</strong> © All Rights Reserved. <!--Developed by the <a href="www.Hireatent.com">Frequency Themes --></a></p>
</footer>
</div>
</div>
</body>
</html>
答案 0 :(得分:1)
这里有一些问题,所以请仔细检查我的整个答案。
首先,整个代码的执行依赖于它所设置的条件语句:
if(isset($_POST["submit"])){...}
它正在寻找并依赖于同名的命名属性,这很可能是你的提交按钮:
<button type="submit"></button>
需要命名为:
<button type="submit" name="submit"></button>
或使用具有name属性的输入类型:
<input type="submit" name="submit" value="Submit">
然后你有一个未定义的变量$email2
,很可能需要$email
,按照:
$email=$_POST['email'];
使用错误报告后,会发出“未定义索引提交...”警告,以及“未定义变量email2 ......”。
将error reporting添加到文件的顶部,这有助于查找错误。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
旁注:错误报告应仅在暂存时完成,而不是生产。
现在,这一行:
mail("[ email removed ]", $message, $headers);
缺少“subject”参数,这是使用mail()
时的一个重要参数。
即:
mail("[ email removed ]", $subject, $message, $headers);
因此,您需要为其添加变量。
I.e。:
$subject = "Form submission";
有关mail()
和标题的详细信息,请访问:
从手册:
bool mail(string $ to,string $ subject,string $ message [,string $ additional_headers [,string $ additional_parameters]])
要检查mail()
是否已实际执行,请更改:
mail("[ email removed ]", $message, $headers);
到
if(mail("[ email removed ]", $message, $headers)){
echo "Mail has been sent.";
}
else{
echo "Error. Check your mail logs.";
}
如果/当您看到“邮件已发送。”,则mail()
已完成其工作。
如果您没有收到邮件,请检查您的垃圾邮件,或者如果您在托管网站上,请联系您的托管公司。
如果您是从自己的计算机上运行它,请确保PHP和邮件实际上已正确安装,运行和配置。
<强>脚注:强>
您正在使用以下两次,其中一个可以安全删除:
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
修改强>
要向消息添加其他表单变量,请先声明POST变量:
$name=$_POST['name'];
$email=$_POST['email'];
$tel=$_POST['tel'];
然后将$message = $_POST['note'];
更改为$comment = $_POST['note'];
然后执行:
$message = "$name\n$email\n$tel\n$comment";
\n
添加换行符。