2017-06-22 38 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>