2017-06-22 77 views
0

我有一個表單發送一個訂單號的電子郵件我想將用戶轉到一個感謝頁面並顯示一條消息「」發送的郵件謝謝你的名字,我們會聯繫您聯繫您的訂單號:」 send_email.php此發送電子郵件和用戶轉移到謝謝頁:首先,我做了一些錯誤檢查,如果沒有錯誤我發送電子郵件php - 標題('Location')的郵件

<?php 

function isInjected($str) { 
    $injections = array('(\n+)', 
    '(\r+)', 
    '(\t+)', 
    '(%0A+)', 
    '(%0D+)', 
    '(%08+)', 
    '(%09+)' 
    ); 
    $inject = join('|', $injections); 
    $inject = "/$inject/i"; 
    if(preg_match($inject,$str)) { 
     return true; 
    } 
    else { 
     return false; 
    } 
} 

function test_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 
$nameErr = $lastNameErr = $emailErr = $ironingErr = $descriptionErr = $RoomErr = NULL; 
$first_name = $last_name = $email = $ironing = $description = $Rooms =""; 

if(isset($_POST['submit'])){ 
    $from = $_POST['email']; // this is the sender's Email address 
    $first_name = $_POST['first_name']; 
    $last_name = $_POST['last_name']; 
    $ironing = $_POST['ironing']; 
    $Rooms = $_POST['Rooms']; 
    $Hours = $_POST['Hours']; 
    $Description = $_POST['description']; 
    // If email injection is detected, redirect to the error page. 
    if (isInjected($from)) { 
    header("Location: $error_page"); 
} 

    if (empty($_POST["first_name"])) { 
      $nameErr = "Name is required"; 
     } else { 
      $name = test_input($_POST["first_name"]); 
      // check if name only contains letters and whitespace 
     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"]); 
        // check if e-mail address is well-formed 
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
         $emailErr = "Invalid email format"; 
        } 
       } 
     if (empty($_POST["description"])) { 
      $descriptionErr = "Description is required"; 
        } else { 
        $description = test_input($_POST["description"]); 
        } 
     if (empty($_POST["Rooms"])) { 
       $RoomErr = "Room number is Required"; 
        } else { 
        $Rooms = test_input($_POST["Rooms"]); 
        } 
        if (empty($_POST["Hours"])) { 
       $HourErr = "Room number is Required"; 
        } else { 
        $Hours = test_input($_POST["Rooms"]); 
        } 
        if ($_POST["Hours"] < 3) { 
       $RoomErr = "Mininum number of Hours : 3"; 
        } else { 
        $Rooms = test_input($_POST["Rooms"]); 
        } 

     if (empty($_POST["ironing"])) { 
      $ironingErr = "Ironing is Required"; 
      } else{ 
        $ironing = test_input($_POST["ironing"]); 
      } 
    if (isset($nameErr) || isset($lastNameErr) || isset($emailErr) || 
    isset($ironingErr) || isset($descriptionErr) || isset($RoomErr)) { 

    // You have an error 
} else { 
    $today = date("Ymd"); 
    $rand = strtoupper(substr(uniqid(sha1(time())),0,4)); 
    $unique = $today . $rand; 
    $to = "someemail.com"; // this is your Email address 
    $subject = "Order Sumbittion: " . $unique; 
    $subject2 = "Copy of your form submission"; 
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['description']. "\n\n" . "Number of Rooms: ". $_POST['Rooms'] . "\n\n" ."Number of Hours : ".$_POST['Hours'] . "\n\n" ."Ironing: " . $_POST['ironing']; 
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['description']. "\n\n" . "Number of Rooms: " . $_POST['Rooms']. "\n\n" . "Number of Hours : " . $_POST['Hours'] . "\n\n" ."Ironing: ". $_POST['ironing'] ."\n\n"."Your Odred Number = ". $unique . "\n\n". "Thank you for your Order our Team will be in contact with you shortly."."\n\n". "Check Our facebook page at facebook.com/GlossyCleaningService" ; 
    $headers = "From:" . $from; 
    $headers2 = "From:" . $to; 
    mail($to,$subject,$message,$headers); 
    mail($from,$subject2,$message2,$headers2); 

    // sends a copy of the message to the sender 
    header("Location:thank_you.php?first_name={$first_name}"); 
    exit; 
} 

} 


?> 

在我thank_you .php頁面我有以下的php腳本

<?php 
    include ('send_mail.php'); 
if(isset($_GET['first_name'])) { 
echo "Mail Sent. Thank you " . $_GET['first_name'] . ", we will contact you shortly."; 
} 
?> 

我得到的消息我的網頁是

「通知:未定義的變量:在/storage/ssd1/717/1954717/public_html/thank_you.php獨特的上線126 郵寄。謝謝你,我們會盡快與您聯繫,您的訂單號:

該網頁犯規打印出來的名字或訂單號現在看來似乎無法找到它

更新:我改變了我代碼凱文·納爾遜回答我沒有得到任何錯誤或消息只是空白

檢查這裏的問題:。mysite

+0

嘗試在會話中存儲訂單ID?通過使用標題('Location ...');你重定向用戶並且變量丟失 –

+1

在send_mail.php文件中沒有'$ first_name'變量,除非你沒有向我們展示所有東西。 '頭: – TripleDeal

+0

如果你不使用會話和訂單ID沒有被隱藏,可以通過在GET VAR URL傳遞它:;' – Kaddath

回答

3

你的問題是你重定向而不發送URL中的參數。如果您想獲得該數據,則需要在重定向中執行header("Location:thank_you.php?first_name=X")等。例如

$first_name = /** WHAT??? You don't have this in your code so I can't verify it exists **/ 

$today = date("Ymd"); 
$rand = strtoupper(substr(uniqid(sha1(time())),0,4)); 
$unique = $today . $rand; 
$_SESSION['unique'] = $unique; 
mail($to,$subject,$message,$headers); 
mail($from,$subject2,$message2,$headers2); 
// sends a copy of the message to the sender 
header("Location:thank_you.php?first_name={$first_name}&unique={$unique}"); 
exit; 

要訪問此下一個頁面上,你會怎麼做:

<?php 
if(isset($_GET['first_name']) && isset($_GET['unique'])) { 
    echo "Mail Sent. Thank you " . $_GET['first_name' . ", we will contact you shortly.". ", Your Order Number:". $_GET['unique']; 
} 

否則,它可能只是簡單的包括thank_you.php頁隨後將有機會獲得所有變量within scope

$first_name = /** WHAT??? You don't have this in your code so I can't verify it exists **/ 

$today = date("Ymd"); 
$rand = strtoupper(substr(uniqid(sha1(time())),0,4)); 
$unique = $today . $rand; 
mail($to,$subject,$message,$headers); 
mail($from,$subject2,$message2,$headers2); 
// include the other file 
include('thank_you.php'); 
exit; 

在這種情況下,thank_you.php是:

<?php 
    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.". ", Your Order Number:". $unique; 
?> 

UPDATE,按您的問題...如果你有問題,試試這個:

php1.php

<?php 

    header("Location: php2.php?first_name=Joe"); 
    exit; 

php2.php

<?php 

    echo $_GET['first_name']; 

如果還是不行工作,你有你的PHP環境的問題不像我的那樣工作。如果有效,那麼問題出在您的代碼上。

+0

這仍然會將用戶重定向到thank_you.php頁面? –

+0

不,這將在正在執行的頁面上顯示「thank_you.php」頁面......它是「而不是」重定向。如果你想「重定向」,以便用戶的URL改變,那麼你會想使用我建議的'?first_name = X'。 –

+0

我看到我試圖FIRST_NAME = $ FIRST_NAME和我得到現在這個錯誤'注意事項:未定義指數:FIRST_NAME在上線/storage/ssd1/717/1954717/public_html/thank_you.php 126'線126 ='回聲「已發送郵件。 謝謝 」 。 $ first_name。 「,我們會盡快與您聯繫。」;' –

1

HTTP是無狀態這意味着,一個頁面不知道另一頁的任何作爲結果,任何變量你在第一頁上設置的時間早在您重定向到第二頁時就消失了。如果您希望將操作跨越兩個不同的頁面,則需要將值保存在數據庫中,或者session或通過GET參數在URL上。

0

嘗試這一個

<?php 
$today = date("Y:m:d"); 
$rand = strtoupper(substr(uniqid(sha1(time())),0,4)); 
$unique = $today . $rand; 
header("location:header1.php?unique=$unique"); 
?> 

thank_you.php

<?php 
    echo $_REQUEST['unique']; 
?> 
+0

OP爲什麼要「嘗試這一個」?一個好的答案**將總是解釋所做的事情以及爲什麼這樣做,不僅是爲了OP,而且是爲了將來SO的訪問者。 –

+0

謝謝@bub我會爲此嘗試。 – Bhargav

0

該header()函數發送一個原始HTTP標頭到客戶端。 您可以使用會話狀態將數據傳送到下一個頁面 注意到header()函數的任何實際的輸出被髮送之前,必須調用(在PHP 4及更高版本,可以使用輸出緩存來解決這個問題)是非常重要的

嘗試這樣做,而不是:

$today = date("Ymd"); 
    $rand = strtoupper(substr(uniqid(sha1(time())),0,4)); 
    $unique = $today . $rand; 
    $_SESSION['unique'] = $unique; 
    mail($to,$subject,$message,$headers); 
    mail($from,$subject2,$message2,$headers2); 
    // sends a copy of the message to the sender 
    header('Location:thank_you.php'); 
    exit; 

這將解決您的問題,它不是你設置的變量名這樣反而試圖將它放在會話,然後通過頭功能。現在,我將$ unique變量設置爲會話,您可以將其設置爲任何您想要的,並可以通過提取會話在thankyou.php頁面上使用它。

+2

他沒有在內容後發送頭文件的問題......他的問題是,thank_you.php不知道前一頁中的變量是什麼。 –

+0

是的,所以他可以在會話中設置值,以便它可以在thankyou.php頁面中重複使用它。對於演示,我添加了$ unique變量 –

+0

您使用會話來處理需要在多個請求中維護狀態的事件,而不是一個重定向...因此這是不好的做法。然而,改變你的第一句話來說一些關於「你可以使用會話狀態將數據傳輸到下一頁」,這樣我們就知道這是你解決實際問題的方法。關於header()的評論應該放在帖子的底部作爲旁註,因爲OP的代碼在內容熄滅之前顯然發送了一個標題,並且不適用於...因此,我差點讓你失望了。 –

0

我把它用$ _SESSIONS工作。感謝您的所有答案,對我最合適。這是我做的,我在thank_you.php頁,我沒有加入

$_SESSION['first_name'] = $first_name; 
$_SESSION['unique'] = $unique; 

的代碼

$today = date("Ymd"); 
    $rand = strtoupper(substr(uniqid(sha1(time())),0,4)); 
    $unique = $today . $rand; 
    $to = "[email protected]"; // this is your Email address 
    $subject = "Order Sumbittion: " . $unique; 
    $subject2 = "Copy of your form submission"; 
    $_SESSION['first_name'] = $first_name; 
    $_SESSION['unique'] = $unique; 
    $message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['description']. "\n\n" . "Number of Rooms: ". $_POST['Rooms'] . "\n\n" ."Number of Hours : ".$_POST['Hours'] . "\n\n" ."Ironing: " . $_POST['ironing']; 
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['description']. "\n\n" . "Number of Rooms: " . $_POST['Rooms']. "\n\n" . "Number of Hours : " . $_POST['Hours'] . "\n\n" ."Ironing: ". $_POST['ironing'] ."\n\n"."Your Odred Number = ". $unique . "\n\n". "Thank you for your Order our Team will be in contact with you shortly."."\n\n". "Check Our facebook page at facebook.com/GlossyCleaningService" ; 
    $headers = "From:" . $from; 
    $headers2 = "From:" . $to; 
    mail($to,$subject,$message,$headers); 
    mail($from,$subject2,$message2,$headers2); 

    // sends a copy of the message to the sender 
    //echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.". ", Your Order Number:". $unique; 
    header("Location: thank_you.php"); 
    exit; 

然後。

<h1 class="display-3">Thank You <?php echo $_SESSION['first_name'];?></h1> 
    <p class="lead"><strong>Your order Number :<?php echo $_SESSION['unique'];?></strong> <br> 
    Thank You for your order Our Team Will get back to you Shortly.</p> 
    <hr> 
    <p>