简体   繁体   English

联系表格:提交后用户未返回表格

[英]Contact Form: user not returned to the form after Submit

When a user has clicked on SUBMIT button on Contact Form, success or error message does not show up on the bottom part of the Contact form; 当用户单击联系表单上的“提交”按钮时,成功或错误消息不会显示在联系表单的底部; instead the message is displayed on a new blank page in a plain format and then it terminates; 相反,该消息以纯格式显示在新的空白页上,然后终止。 the user is not returned to the Contact Form. 用户不会返回到联系表单。 I have these three files for the contact form: contactus.html, style.css , and emailform.php . 我有联系表的以下三个文件: contactus.html, style.cssemailform.php

The emailform php (this php resides on the host server) contains the if ($_POST['submit']) { code for error trapping. emailform php(此php驻留在主机服务器上)包含if ($_POST['submit']) {错误捕获代码。 Contactus.html contains Contactus.html包含

<form method="post" action="http://www.xxxxxx.com/emailform.php" name="Form" id="Form"> . <form method="post" action="http://www.xxxxxx.com/emailform.php" name="Form" id="Form">

Can anyone help fix this problem? 谁能解决这个问题? Thanks in advance. 提前致谢。

If after submitting the form you want to send the user back to the previous page your emailform.php file should contain: 如果在提交表单后您想要将用户发送回上一页,则您的emailform.php文件应包含:

header('Location: ' . $_SERVER['HTTP_REFERER']);

If you want to display an error message on contactus.html you could add a parameter to the url let's say contactus.html?error=1 , there you can capture the error and display it to the user within the same page. 如果要在contactus.html上显示错误消息,可以在URL中添加一个参数,例如contactus.html?error=1 ,您可以在其中捕获错误并将其显示在同一页面中。

Alternatively your contactus.html file could do an AJAX request to emailform.php and display the result within the same page so the browser doesn't need to redirect to emailform.php . 或者,您的contactus.html文件可以对emailform.php进行AJAX请求,并在同一页面中显示结果,因此浏览器无需重定向到emailform.php

If you have included jQuery and want to do the POST via AJAX try removing the action=... from your form, make you your head includes jQuery and past this anywhere within your body: 如果您已包含jQuery,并希望通过AJAX进行POST,请尝试从表单中删除action=... ,让您的头包含jQuery,并将其放在体内的任何位置:

<script type="text/javascript">
$("#submit").on("click",function(){

    var email = $("#email").val();
    var name = $("#name").val();
    var message = $("#message").val();

    $.ajax({
        type: "POST",
        url: "/emailform.php",
        data: { email : email, name : name, message: message }
    }).done(function(msg) {
        //update your message div here
        $("#message").test(msg);
        alert(msg);
    });
});

</script>

Do a redirect from your emailform.php back to the contactus.html. 从您的emailform.php重定向回contactus.html。 If you want to be able to react to the form, make the contactus a php-file (if your webserver is not configured to work with php in html files). 如果您希望对表单做出反应,请将contactus设为php文件(如果未将Web服务器配置为使用html文件中的php)。

You can send a get-parameter via url to the contactus when you are redirecting from emailform.php. 从emailform.php重定向时,您可以通过url将get参数发送给联系人。 With header('Location: ' . $_SERVER['HTTP_REFERER'] . '?success=true');

In your contactus.php you can check if there is a parameter etc. 在您的contactus.php中,您可以检查是否有参数等。

if(isset($_GET['success'])){
    if($_GET['success'] === 'true'){
        echo 'it worked fine';
    else{
        echo 'something did not work';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM