0

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

PeterKA
  • 24,158
  • 5
  • 26
  • 48
Ross Coulbeck
  • 602
  • 1
  • 9
  • 22
  • Javascript runs Client Side, PHP runs Server Side. When you use the location tag it is sending you to a new PHP page, essentially reloading the contact page. This means any client side scripting that was executed after the first page load has been wiped out, and you are back to the original page load state. – Mark Jul 02 '14 at 15:01
  • Thanks. I understand that, which is why I'm hoping there is a better way to do this. Any ideas? – Ross Coulbeck Jul 02 '14 at 15:06
  • Look at Matt The Ninja's solution below. He is suggesting that you submit the form to PHP every time, then when you are in the PHP script you can validate and display the thank you, or fail validation and display an error about what went wrong, and you avoid the messy client side processing issues. – Mark Jul 02 '14 at 15:08

5 Answers5

3

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")
 }
}
Matt The Ninja
  • 2,641
  • 4
  • 28
  • 58
  • 1
    The advantage to this approach is that you would move your Javascript validation code to the PHP side, which is inherently more secure as it will run on the server, and not on the client where even an amateur web developer could screw with the code. – Mark Jul 02 '14 at 15:06
  • More secure yes, but this doesn't solve my issue with displaying a thank you message, and displaying errors from the php is more difficult. In Javascript I can just display errors straight onto the page. I have no doubt this is a better more secure way however. – Ross Coulbeck Jul 02 '14 at 15:15
  • You can still display a thank you message. Just display it as part of the if statement if data is posted correctly (or redirect to a thank you page). Displaying errors from php shouldn't be difficult see here http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Matt The Ninja Jul 02 '14 at 15:19
  • I'll have a look into it. I think you misunderstood what I meant by error, I mean user errors e.g. invalid email. – Ross Coulbeck Jul 02 '14 at 15:21
  • Ah sure, but you are handling these with JS before submitting the form right? You are only posting the form IF the validation is passed? – Matt The Ninja Jul 02 '14 at 15:22
  • Correct, but from Marks comment I thought this was a replacement for that. I think what I'm going to do is just create a thank you page and re-direct it to that, seems like the simplest solution. Thanks – Ross Coulbeck Jul 02 '14 at 15:23
  • Certainly can redirect to a thank you page. Will work the same just means more changes. other IF (POST)[submit and show thank you]ELSE[show form] make sense? – Matt The Ninja Jul 02 '14 at 16:33
1

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
}
Tmb
  • 450
  • 12
  • 20
0

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.

Valentin Mercier
  • 5,256
  • 3
  • 26
  • 50
0

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>
Jnatalzia
  • 1,687
  • 8
  • 14
  • Sounds like a good idea, how would I add a parameter to the header? – Ross Coulbeck Jul 02 '14 at 15:08
  • Well above, I'm just checking to see if the form was submitted by seeing if the `submit_btn` named element is available in the request. So that's what I mean by a submission parameter. Anytime the person submits the form, that parameter will be passed to the page on submission – Jnatalzia Jul 02 '14 at 15:09
0

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.

Ross Coulbeck
  • 602
  • 1
  • 9
  • 22