简单的PHP表单重定向

时间:2011-02-09 10:44:45

标签: php

我正在尝试在此表单上创建一个简单的重定向。单击提交时表单会发送一封电子邮件,但不会重定向到我指定的网址。

这是我的PHP代码:

<?php if ($valid_message)
{
header("Location: http://www.beulahprint.ie/energycentre.php");
exit();
}
else {print "We encountered an error sending your mail"; }
    $to = "colm@beulahprint.ie";
    $subject = "Web Contact Form";

    $email = $_REQUEST['email'] ;
    $message = $_REQUEST['message'] ;

    $headers = "From: $email";
    $sent = mail($to, $subject, $message, $headers) ;
?>

3 个答案:

答案 0 :(得分:2)

您的代码似乎无序。首先,发送邮件。然后,如果成功,重定向。您尝试重定向然后发送邮件。

<?php
    $to = "colm@beulahprint.ie";
    $subject = "Web Contact Form";

    $email = $_REQUEST['email'] ;
    $message = $_REQUEST['message'] ;

    $headers = "From: $email";
    $sent = mail($to, $subject, $message, $headers) ;

    if ($sent) {
      header("Location: http://www.beulahprint.ie/energycentre.php");
      exit();
    } else {
      print "We encountered an error sending your mail"; 
    }

?>

答案 1 :(得分:0)

<?php
    $to = "colm@beulahprint.ie";
    $subject = "Web Contact Form";
    $email = $_REQUEST['email'] ;
    $message = $_REQUEST['message'] ;
    $headers = "From: $email";
    $sent = mail($to, $subject, $message, $headers) ;

    if ($sent) {
      header("Refresh: 0; URL=http://www.beulahprint.ie/energycentre.php");
      exit();
    } else {
      echo "We encountered an error sending your mail"; 
    }
?>

答案 2 :(得分:0)

You can make use of header function after checking status of email send. First send the email on click of submit button code like below -

if (isset($_REQUEST['btn_submit'])) {
  $to = $_REQUEST['strAddressTo'];
  $subject = $_REQUEST['strSubject'];

  $email = $_REQUEST['strEmail'] ;
  $message = $_REQUEST['strMessage'] ;

  $headers = "From: $email";
  $sent = mail($to, $subject, $message, $headers) ;

  if ($sent) {
    header("Location: http://your_desired_location/page.php");
    exit();
  } else {
    die("We have faced some error");
  }
}

?>

Instead of making use $_REQUEST use specific method like Get or Post.