php - 带有消息的标题('Location')

时间:2017-06-22 13:46:13

标签: php html

我有一个表单,发送带有订单号的电子邮件我想将用户转移到感谢页面并显示消息“”发送邮件谢谢您的名字我们会尽快与您联系您的订单号:“send_email。 php发送电子邮件并将用户转移到感谢页面:首先我做一些错误检查,如果没有错误我发送电子邮件

<?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.";
}
?>

我在网页上收到的消息是

“注意:未定义的变量:第126行的/storage/ssd1/717/1954717/public_html/thank_you.php中的唯一 邮件已发送。谢谢,我们会尽快与您联系。,您的订单号:

网页没有打印出它看起来无法找到的名字或订单号。

更新:我将我的代码更改为Kevin Nelson答案我没有收到任何错误或消息只是空白

检查此处的问题:mysite

5 个答案:

答案 0 :(得分:3)

您的问题是您正在重定向而不在URL中发送参数。如果您想要提供这些数据,则需要在重定向中执行header("Location:thank_you.php?first_name=X")等操作。 e.g。

$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;
?>

更新,根据您的问题......如果您遇到问题,请尝试以下方法:

php1.php

<?php

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

php2.php

<?php

    echo $_GET['first_name'];

如果这不起作用,那么您的PHP环境就会像我的一样无法正常工作。如果它有效,则问题出在你的代码上。

答案 1 :(得分:1)

HTTP是无状态的。这意味着一个页面对另一个页面一无所知。因此,您在第一页上设置的任何变量在重定向到第二页时都会消失。如果您希望操作跨越两个不同的页面,那么您需要保留值,可能在数据库中,或session,或通过GET参数在URL上。

答案 2 :(得分: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'];
?>

答案 3 :(得分: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变量设置为session,您可以将其设置为您想要的任何值,并可以通过提取会话在thankyou.php页面上使用它。

答案 4 :(得分:0)

我使用$ _SESSIONS让它工作。感谢您的所有答案,最适合我。这是我做的,我补充说。

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

代码

$today = date("Ymd");
    $rand = strtoupper(substr(uniqid(sha1(time())),0,4));
    $unique = $today . $rand;
    $to = "tadzik9634@gmail.com"; // 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;

然后在我的thank_you.php页面中,我做了。

<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>