我有这个代码,我从其他脚本和我在互联网上找到的东西放在一起,
不知何故,当我刚刚加载页面时,它发送了两封电子邮件,一封电子邮件,当我提交时,它就是第二封电子邮件。
此外,header()
并未将我发送到我想要的页面...它只是停留在同一个表单页面上,如果有人可以帮我查明发生了什么,那就太多了赞赏,我认为它确实与帖子有关,但我不能因为我的爱而弄明白!
谢谢
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<style>
.error {color: #FF0000;}
h6
{
font-family: bookman old style;
font-size:20px;
text-align: center;
font-weight: normal;
}
h5
{
font-family: bookman old style;
font-size:15px;
text-align: center;
font-weight: normal;
}
</style>
<?php
$nameErr = $emailErr = $websiteErr = $categoryErr = "";
$name = $email = $comment = $website = $category = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["website"])) {
$websiteErr = "URL is required";
} else {
$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["category"])) {
$categoryErr = "Category is required";
} else {
$category = test_input($_POST["category"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
<?php include'header.php'?>
<h6>Link Submission</h6>
<h5><span class="error">* required field.</span>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name Of Site: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
URL: <input type="text" name="website" value="<?php echo $website;?>">
<span class="error">* <?php echo $websiteErr;?></span>
<br><br>
Description: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
<br><br>
Category Of Site: <select size="1" name="category">
<option value="<?php echo $category;?>"> -- Please select -- </option>
<option>Arts</option>
<option>Business</option>
<option>Computers</option>
<option>Games</option>
<option>Health</option>
<option>Home</option>
<option>Kids and Teens</option>
<option>News</option>
<option>Recreation</option>
<option>Reference</option>
<option>Science</option>
<option>Shopping</option>
<option>Society</option>
<option>Sports</option>
<option>World</option>
</select><span class="error">* <?php echo $categoryErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</h5><?php include'footer.php'?>
<?php
$myemail = "links@loadsofads.com";
$subject = "Link Submission";
$message = "Your Link Submission form has been submitted by:
Website Name: $name
E-mail: $email
URL: $website
Category: $category
Description:
$comment";
mail($myemail, $subject, $message);
header('Location: submitthanks.php');
?>
答案 0 :(得分:2)
它向您发送了两封电子邮件,因为您需要在条件语句中设置整个代码。
将isset()
与已命名的提交按钮结合使用,该按钮仅在单击提交按钮后发送邮件,而不是在页面加载时发送。
<input type="submit" name="submit" value="Submit">
修改为:
<?php
if(isset($_POST['submit'])){
$myemail = "links@loadsofads.com";
$subject = "Link Submission";
$message = "Your Link Submission form has been submitted by:
Website Name: $name
E-mail: $email
URL: $website
Category: $category
Description:
$comment";
mail($myemail, $subject, $message);
header('Location: submitthanks.php');
exit;
}
关于标题不重定向是因为您在标题之前输出,如果已设置/ error reporting,则会发出Headers already sent...
警告。
在页面顶部添加ob_start();
并在<?php
?>
标记内设置有时会有所帮助,并置于<!DOCTYPE html...
即:
<?php ob_start(); ?>
<!DOCTYPE html ...
最好将表单操作用于其他页面,而不是放在同一页面上,并将邮件代码放在该文件中。
如果您希望使用现有代码而不使用第二页作为邮件处理程序,则另一种选择是使用元刷新方法。
例如,代替header()
:
$url = "submitthanks.php";
print "<meta HTTP-EQUIV=Refresh CONTENT=\"0; URL=$url\">";
编辑: - 重写#2
请务必将此行$myemail = "email@example.com";
更改为您的电子邮件地址。
另外,还有一个mail()
header丢失,很可能会向垃圾邮件发送邮件,
并添加了from Name
,以便更加个性化。
<?php
ob_start(); // prevents headers already sent warning
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<style>
.error {color: #FF0000;}
h6
{
font-family: bookman old style;
font-size:20px;
text-align: center;
font-weight: normal;
}
h5
{
font-family: bookman old style;
font-size:15px;
text-align: center;
font-weight: normal;
}
</style>
<?php
$nameErr = $emailErr = $websiteErr = $commentErr = $categoryErr = "";
$name = $email = $comment = $website = $category = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
$Error = 1;
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
$Error = 1;
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
$Error = 1;
}
}
if (empty($_POST["website"])) {
$websiteErr = "URL is required";
$Error = 1;
} else {
$website = test_input($_POST["website"]);
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
}
if (empty($_POST["comment"])) {
$commentErr = "Comment is required";
$Error = 1;
} else {
$comment = test_input($_POST["comment"]);
}
if ($_POST["category"] == "" ) {
$categoryErr = "Category is required";
$Error = 1;
} else {
$category = test_input($_POST["category"]);
}
} // brace for if ($_SERVER["REQUEST_METHOD"] == "POST")
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
<?php include 'header.php'; ?>
<h6>Link Submission</h6>
<h5><span class="error">* required field.</span>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name Of Site: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
URL: <input type="text" name="website" value="<?php echo $website;?>">
<span class="error">* <?php echo $websiteErr;?></span>
<br><br>
Description: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea><span class="error">* <br><?php echo $commentErr;?></span>
<br><br>
Category Of Site: <select size="1" name="category">
<option value="<?php echo $category;?>"> -- Please select -- </option>
<option>Arts</option>
<option>Business</option>
<option>Computers</option>
<option>Games</option>
<option>Health</option>
<option>Home</option>
<option>Kids and Teens</option>
<option>News</option>
<option>Recreation</option>
<option>Reference</option>
<option>Science</option>
<option>Shopping</option>
<option>Society</option>
<option>Sports</option>
<option>World</option>
</select><span class="error">* <?php echo $categoryErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</h5><?php include 'footer.php'; ?>
<?php
if(isset($_POST['submit'])){
if ($Error != 1){
$myemail = "email@example.com";
$subject = "Link Submission";
$message = "Your Link Submission form has been submitted by:
Website Name: $name
E-mail: $email
URL: $website
Category: $category
Description:
$comment";
$headers = "From: ". $name . " <" . $email . ">\r\n";
mail($myemail, $subject, $message, $headers);
header('Location: submitthanks.php');
} // brace for if ($Error != 1)
} // brace for if(isset($_POST['submit']))
?>
答案 1 :(得分:0)
您需要将这部分代码放在帖子部分
中if ($_SERVER["REQUEST_METHOD"] == "POST"){
$myemail = "links@loadsofads.com";
$subject = "Link Submission";
$message = "Your Link Submission form has been submitted by:
Website Name: $name
E-mail: $email
URL: $website
Category: $category
Description:
$comment";
mail($myemail, $subject, $message);
header('Location: submitthanks.php');