为什么我的 Form POST 将我带到一个新页面


Why is my Form POST taking me to a new page?

Setup

我有一个简单的HTML表单,指向将发送电子邮件的PHP脚本。

问题

当我单击提交时,电子邮件已发送,但我被带到PHP网址。

我尝试在和声明中的所有 TARGET="_self",但我仍然被带到了一个新的页面。

法典网页代码

<form name="contactform" method="post" action="send_form_email1.php">
    <table width="450px">
        <tr>
            <td>
                <input type="text" placeholder="Full Name" name="full_name"/>
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" placeholder="Email Address" name="email"/>
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" placeholder="Phone Number" name="telephone"/>
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" placeholder="Question" name="comments"/>
            </td>
        </tr>
        tr>
                 <td colspan="2" style="text-align:center">
                <input type="submit" value="Submit" > 
            </td>
        </tr>
    </table>
</form>

PHP脚本

// EDIT THE 2 LINES BELOW AS REQUIRED
 // EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "zachmurray13@gmail.com";
$email_subject = "Your email subject line";

function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}
// validation expected data exists
if(!isset($_POST['full_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['telephone']) ||
    !isset($_POST['comments'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
}
$full_name = $_POST['full_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+'.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$full_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.'n'n";
function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}
$email_message .= "Last Name: ".clean_string($last_name)."'n";
$email_message .= "Email: ".clean_string($email_from)."'n";
$email_message .= "Telephone: ".clean_string($telephone)."'n";
$email_message .= "Comments: ".clean_string($comments)."'n";
// create email headers
$headers = 'From: '.$email_from."'r'n".
'Reply-To: '.$email_from."'r'n" .
'X-Mailer: PHP/' . phpversion();
 @mail($email_to, $email_subject, $email_message, $headers);  
?>

<?php
}
?>

任何帮助将不胜感激。

Post 的工作原理是将数据发送到表单操作中指定的页面。 您需要将浏览器重定向回所需的页面。 您可以通过 302 重定向来执行此操作。

header('Location: http://www.yoursite.com/home-page.html');
exit;

如果你不想去新的URL,那么你可以向php脚本发出AJAX请求,而不是表单提交。

表单元素的作用是将数据发送到不同的页面。 正如您在操作中指定的那样。

如果你想回到实际的

  1. 您必须重定向到它。
  2. 您还可以将send_form_email1.php文件包含在实际文件中。

例如:

header('Location: page_url');

include('yourfile');

如果要将文件包含在实际文件中,则必须将action="设置为空白,它会自动将其发送到同一页面,并且必须检查为POST请求。然后其他代码将保持不变。

您的浏览器将在表单发布到页面时加载页面。这是预期行为。

您可以使用一些选项解决此问题:

  1. 帖子是在同一页面上完成的,处理表单的 PHP 代码是其中的一部分。检查if ($_POST)以了解用户正在提交某些内容,但页面无论如何都会重新加载;

  2. 表单正在调用的页面会将用户重定向到其他页面;

  3. 使用 AJAX 发布它。这样,您甚至不会丢失字段内容,页面根本不会刷新。

选项 1 不是首选,因为如果用户在提交表单后尝试重新加载页面,浏览器将再次提交。避免这种情况的最佳做法是重定向用户,除非您使用 AJAX 发布,在这种情况下,您可以完全控制发布的任何内容,它甚至不会用它们填充浏览器历史记录。

我认为使用标题将是不错的选择。

header('你的html页面的位置');在你的php页面处理表单。

还有另一种选择没有广泛使用。

你可以将 php 文件中的代码包含在你的第一页并保存为 php 代码,然后在 form tag 的 action 属性中,请不要输入类似

<form name="contactform" method="post" action="">

这不是推荐的,但是对于您的问题,我强烈建议将其作为最简单的解决方案

你可以尝试几个选项

  1. 您可以尝试ajax - 在同一页面上并使用另一个php文件发送电子邮件
  2. 这将要求您将扩展名从.html更改为.php。您可以将表单自行发布到现有页面。不过,有一些安全漏洞。
  3. 您可以保留现有代码,但在通过 javascript 或 php 标头发送电子邮件后重定向回初始页面。