Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 防止表单提交时重定向_Php_Email_Forms_Redirect - Fatal编程技术网

Php 防止表单提交时重定向

Php 防止表单提交时重定向,php,email,forms,redirect,Php,Email,Forms,Redirect,我最近(在一点帮助下)编写了这个简单的表单到电子邮件脚本。它可以工作,唯一的问题是它在运行时重定向到包含的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="htt

我最近(在一点帮助下)编写了这个简单的表单到电子邮件脚本。它可以工作,唯一的问题是它在运行时重定向到包含的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="form.php">

只有在发送表单(并设置了电子邮件)时,才会执行此部分。

将表单标签更改为:

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

并将您的索引php更改为:

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


因此,如果表单已提交,请发送电子邮件,否则,请显示表单。

您可以直接从form.php重定向回

将其放在form.php的末尾

header("url: index.php");
甚至:

header("url: index.php?sent=true");
在index.php中,您可以

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