1

The submit button is in contact-us.html, the code is in contact-us.php.

I tried to use the header('Location: example'); with no luck... (I tried using the URL of the page as well: header('Location: http://www.example.com/contact-us.html'); )

Any thoughts?

<?php

if($_POST["submit"]) {

    $recipient="example@gmail.com";
    $subject="Form to email message";
    $sender=$_POST["firstname"];
    $senderlast=$_POST["lastname"];
    $senderemail=$_POST["senderemail"];
    $message=$_POST["message"];
    $mailBody="First Name: $sender\nLast Name: $senderlast\nEmail: $senderemail\n\nMessage: $message";

    header('Location: /contact-us.html'); /*Something Wrong?*/

    mail($recipient, $subject, $mailBody, "From: $sender <$senderemail>")
}
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Frosty
  • 39
  • 1
  • 7
  • you want to send the user back to the contact us.html? page after you've sent the email? – Masivuye Cokile Jul 27 '17 at 09:24
  • Yes!! when the button is pressed it goes to the php page and stays there – Frosty Jul 27 '17 at 09:27
  • Does the mail send? Could be that `if($_POST['submit'])` is `false` and the header is never set. – ImClarky Jul 27 '17 at 09:29
  • yes the mail works fine, i can receive all emails. only the redirect bit doesnt work... – Frosty Jul 27 '17 at 09:33
  • You do realize that the header is being send before the mail function, i hope. This will mean that your mail will never be send if this works – Dorvalla Jul 27 '17 at 09:33
  • yes dorvalla is right – Anand Pandey Jul 27 '17 at 09:34
  • where does it go? does it stays blank or give an error? is your php page not in the same directory as the html? – Masivuye Cokile Jul 27 '17 at 09:34
  • you have to use mail upper and header in after mail function and without / also. – Anand Pandey Jul 27 '17 at 09:37
  • i moved the header in different places before.. it looks like it is being skipped.. i do receive the emails which means that the mail part works... – Frosty Jul 27 '17 at 09:43
  • no errors or something, just stays at a blank page example.com/contact-us.php . and yes both files are in the same location/directory – Frosty Jul 27 '17 at 09:44
  • @Dorvalla - That's actually not true. The code will continue to execute unless you explicitly end execution (with `exit;` for example). As explained [in this answer](https://stackoverflow.com/a/30430945/3760604). – ImClarky Jul 27 '17 at 10:45

5 Answers5

0

Have you tried to look towards the referer? PHP has a function for that.

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

This means that if it comes from the cnotact.html, it will look how it came there and refers back. Easy to use if you have a second contact form on another page.

I need to add that though that this may not work via secure pages. (so if your run it via https) and its possible that the header may be able to be hijacked (especially if the browser did not send the request).

Dorvalla
  • 5,027
  • 4
  • 28
  • 45
  • 1
    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. – Masivuye Cokile Jul 27 '17 at 09:28
  • I am aware of this, (hence i modified the answer a tat) but the litteral answer to this question is just that – Dorvalla Jul 27 '17 at 09:30
0

Here's how I'd do it:

I recommend using $_SESSION to store previous url like this:

page1.php

<?php
    $_SESSION['previous'] = 'http://'. $_SERVER[HTTP_HOST]. $_SERVER[REQUEST_URI];

page2.php

<?php
    if ($_SESSION['previous'] !== '') {
        header('Location: '. $_SESSION['previous']);
    }

$_SESSION kind of works like cookies, but a lot better. Easier to use as it's just using an array - more info can be found here: http://uk1.php.net/manual/en/reserved.variables.session.php

treyBake
  • 6,440
  • 6
  • 26
  • 57
0

Are you sure you have a button with the submit name attribute? if yes maybe try this :

<?php
if($_POST["submit"]) {

    $recipient="example@gmail.com";
    $subject="Form to email message";
    $sender=$_POST["firstname"];
    $senderlast=$_POST["lastname"];
    $senderemail=$_POST["senderemail"];
    $header = "MIME-Version: 1.0" . "\r\n";
    $header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $header .= 'From: $sender <$senderemail>' . "\r\n";
    $message=$_POST["message"];
    $mailBody="First Name: $sender\nLast Name: $senderlast\nEmail: $senderemail\n\nMessage: $message";


    if(mail($recipient, $subject, $mailBody, $header)){

        header('Location:contact-us.html'); /*Something Wrong?*/
    }
}

if no then your code never enters the if block

user1
  • 262
  • 1
  • 2
  • 13
0

try using JavaScript window.location to redirect Note : wrap window.location.href('contact-us.html'); into script tag

example :

echo "<script>window.location.href('contact-us.html');</script>";
Machavity
  • 30,841
  • 27
  • 92
  • 100
-1
header('Location:  contact-us.html'); /*Something Wrong?
  • use it without / – ehsan karimi Jul 27 '17 at 09:27
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/16845979) – marsaldev Jul 27 '17 at 11:36
  • @sarcom While it's not a great answer (possibly not even useful), it is an answer. Please read [this meta](https://meta.stackoverflow.com/questions/287563/youre-doing-it-wrong-a-plea-for-sanity-in-the-low-quality-posts-queue) before doing more reviews like this – Machavity Jul 27 '17 at 12:42