页面重定向完成它的工作

时间:2018-03-27 03:00:28

标签: php redirect

我有一个php页面,可以在提交表单后完成工作。表单将数据输入数据库,然后下一页查询数据库和创建者,并根据输入到表单中的信息发送电子邮件。发送电子邮件后,我需要该页面重定向回原始表单。

        <?php
    $con = mysqli_connect('localhost','username','password');

if(!$con)
{
    echo 'Not Connected To Server';
}
if(!mysqli_select_db($con,'dbname'))
{
    echo 'Database Not Selected';
}

$Name = $_POST['Name'];
$Email = $_POST['Email'];

$sql = "INSERT INTO tablename (Name,Email) VALUES ('$Name','$Email')";

if(!mysqli_query($con,$sql))
{
    echo 'Not Inserted';
}
else
{
    echo 'Blah Blah Blah Yackity Schmackity';
    $id = $con->insert_id; // Get latest inserted id.

    $to      = $Email;
    $subject = 'Subject Line Here';
    $message = '<center><img src="http://domainname.com/images/Banner.jpg" /> 
</center><br><br>start of email text here<b> "'.$Name.'"</b> Your Member ID 
is "'.$id.'"Rest of email text here.
';
    $headers = 'From: email@domainname.com' . "\r\n" .
        'Reply-To: email@domainname.com' . "\r\n" .
        'Content-Type: text/html' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);
}   ?>

1 个答案:

答案 0 :(得分:0)

正如先前的评论所述,没有回应。将成功/错误消息保存在会话中,然后将该数据放入要定向的文件中。如手册所述,邮件功能:如果邮件成功接受传递,则返回TRUE,否则返回FALSE。

if(mail($to, $subject, $message, $headers)){
   $_SESSION['msg'] = 'Your mail was sent';
} else {
  $_SESSION['msg'] = 'Your mail was not sent';
}
header('Location: '.$url); exit();

// Response page
echo htmlspecialchars($_SESSION['msg']);
unset($_SESSION['msg']);