簡體   English   中英

防止表單提交上的重定向

[英]Prevent redirection on form submission

我最近(有一點幫助)將這個簡單的表格寫入電子郵件腳本。 它的工作原理,唯一的問題是它在運行時重定向到包含的php腳本。 有沒有人對如何阻止這種情況發生任何想法,而只是在原始頁面上顯示感謝信?

的index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Megaform</title>
</head>

<body>
  <?php include 'form.php' ?>
</body>
</html>

form.php的

<form name="email" method="post" action="email.php">
  <p>
    <label for="name">Full Name</label>
    <input type="text" name="name" id="name">
  </p>
  <p>
    <label for="email">Email Address</label>
    <input type="text" name="email" id="email">
  </p>
  <p>
    <label for="phone">Phone Number</label>
    <input type="text" name="phone" id="phone">
  </p>
  <p>
    <label for="subject">Subject</label>
    <input type="text" name="subject" id="subject">
  </p>
  <p>
    <label for="message">Message<br>
    </label>
    <textarea name="message" id="message" cols="45" rows="5"></textarea>
  </p>
  <p><input type="submit" name="send" id="send" value="Submit"><input type="reset" name="send" id="send" value="Reset">
  </p>
</form>

email.php

<?php  

// Get the Variables
  $name = $_POST['name'];
  $visitor_email = $_POST['email'];
  $phone = $_POST['phone'];
  $subject = $_POST['subject'];
  $message = $_POST['message'];


// Validate against spammers
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;
    }
}

if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}


// Compose the Email
    $email_from = 'a@b.com';  // Set a valid email address that the form can use

    $email_subject = "New Form submission - $subject";  //  Change the message subject here

    $email_body = "You have received a new message from $name ($phone) .\n Here is the message:\n $message";


// Send The Email
  $to = "c@d.com";  // Set a valid email address to send the form to

  $headers = "From: $email_from \r\n";

  $headers .= "Reply-To: $visitor_email \r\n";

  mail($to,$email_subject,$email_body,$headers);

?>
<p>Thankyou for your email.</p>

將表單標記更改為:

<form name="email" method="post" action="index.php">

並改變你的索引php說:

<body>
<?php
    if (isset($_POST['email'])) {
        include 'email.php';
    } else {
        include 'form.php';
    }
?>
</body>

因此,如果表單已提交,請發送電子郵件,否則,請顯示表單。

您需要將表單目標設置為文件itselfe。

<form name="email" method="post" action="form.php">

並在那里包含您的email.php代碼。

if ($_POST['email']) {
  ... code from email.php ...
  ... and success message ...
}

然后,只有在發送表單(並設置了電子郵件)時才會執行此部分。

您可以直接從form.php重定向

把它放在form.php的末尾

header("url: index.php");

甚至:

header("url: index.php?sent=true");

在index.php中你可以擁有

if($_GET['sent']){
   echo "Message Sent.";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM