简体   繁体   中英

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 .

The emailform php (this php resides on the host server) contains the if ($_POST['submit']) { code for error trapping. Contactus.html contains

<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:

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.

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 .

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:

<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. 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).

You can send a get-parameter via url to the contactus when you are redirecting from emailform.php. With header('Location: ' . $_SERVER['HTTP_REFERER'] . '?success=true');

In your contactus.php you can check if there is a parameter etc.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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