2014-10-17 245 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

相關問題