将表单提交到php脚本并返回

I have a contact form which initially "onsubmit" calls a javascript function to validate the content. If this function returns true it then posts the data to a php file.

At the end of this file I would like to return to the contact page, currently I am using this:

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

is there a better way?

I would also like to change the css on an element on the contact page to display when I return (a thank you message), is this possible? I can set it to display using jQuery in the initial javascript function but that gets wiped out when it goes to the php file.

Thanks

Make the form submit the data to the same page of the contact form. Then from the PHP script check if a form has been submitted, do your processing code and return a div with your thank you message.

Say the name of your contact page was index.php. You could setup your form with the correct action (although it should be this way by default) and then check in the header for a submission parameter. Code below

<?php
  if (array_key_exists("submit_btn",$_REQUEST) {
    //display a thank you
  }
?>

<!-- more code here-->

<form action="index.php">
  <input name="whatever" type="text">
  <input type="submit" name="submit_btn">
</form>

Post to self then you if statement after post has been handled to redirect. Do you redirect like you have said, using headers.

So make it post to self on the action part of your form

<form action="yourpage.php">

Then added PHP to handle post on yourpage.php

if(isset($_POST)){
 //Handle POSTed data.

 //if handled correctly
 {
  header("Location: somewhere.com")
 }
}

The HTTP_REFERER is not the best solution to accomplish a history -1 in PHP.

As stated in the documentation :

The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

The best solution is to explicitly write the page to return in the header function.

For the second question, you can redirect with a query parameter:

header('Location: script.php?valid=1');

And then test it in your view:

if (isset($_GET['valid']) && $_GET['valid'] == '1') {
     // display the message
}

I found a few ways to do this in the end thanks to various peoples answers. Thanks all.

The simplest way was just to re-direct to a contact thank you page using the following header:

header("Location: http://{$_SERVER['SERVER_NAME']}/contact-thankyou.html");

I changed to using SERVER_NAME as HTTP_REFERER wasn't a great solution.

The second solution, which was a bit uglier but did the job was to return to the same page with a header like this:

header("Location: http://{$_SERVER['SERVER_NAME']}/contact.html?submitted");

Then use the following Javascript to detect the query parameter and change the css:

if (window.location.search.substring(1) == "submitted")
        {
            $("#message-sent").css({"display": "block"});
        }

This is NOT a great solution, as I'd prefer to get the query parameter using php but I'm restricted to html and Javascript on pages with the template system I'm having to build on.

Thanks all.