2014-10-17 147 views
0

我有这样的代码,我从其他脚本和东西,我发现在互联网上放在一起,PHP发送两封电子邮件

不知怎的,它是给我两封电子邮件,一个电子邮件时我只是加载页面,当然第二当我提交。

此外,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 = "[email protected]"; 
$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

为什么在底部全是你的代码,你应该分开的东西一点点。尝试在重定向后添加'exit()' – meda 2014-10-17 03:23:40

回答

2

,因为你需要设置一个条件语句中整个代码它向您发送两封电子邮件。

isset()与您已命名的提交按钮结合使用,该按钮只会在点击提交按钮后才会发送邮件,而不会在页面加载时发送。

<input type="submit" name="submit" value="Submit"> 

修改于:

<?php 

if(isset($_POST['submit'])){ 

$myemail = "[email protected]"; 
$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 protected]";是你的电子邮件地址。

另外,有一个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 protected]"; 
$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

好吧我尝试了所有这些......每一步......但是,如果用户没有输入,那么一旦你进入表单,你就可以用$ url来代替header()任何需要的东西直接传送到谢谢页面,而另一个ISSET则不起作用。 – 2014-10-17 04:02:19

+0

@ElaineN重新加载并查看**编辑:**它的工作原理。它回答你原来的问题。关于验证,这是另一个故事,我今晚没有时间做一个完整的测试和重写,已经过了午夜,我必须去睡觉。我相信我的编辑将修复重定向问题和我已经测试的页面加载邮件发送。为了快速修复,你可以在要填写的表单元素中添加'required'。 – 2014-10-17 04:12:03

+0

好的谢谢,祝你好运 – 2014-10-17 04:19:12

0

你需要把你的这部分代码在后段

if ($_SERVER["REQUEST_METHOD"] == "POST"){ 
    $myemail = "[email protected]"; 
    $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

我不明白我必须把它放在哪里。 – 2014-10-17 04:03:54

+0

好吧我把它,但现在当验证它发送给我一个空白表单之前,用户的变化进行更改或添加 – 2014-10-17 04:51:20

+0

虐待编辑整个脚本给我20分钟 – 2014-10-17 06:20:18