0

I'm trying to display a simple confirmation message after a PHP contact form has sent an e-mail and redirected to the previous page. I'm redirecting like so:

header("Location: contact.php?mailsend");

In the contact.php I'm trying to display the confirmation this way:

<?php 
  $mailsend = $_GET['mailsend'];
  if ($mailsend == 1){
      echo "<div><p>Your e-mail was sent.</p></div>";
  }
?>

But the page only loads properly if mailsend is present in the URL. When it is not, the page doesn't load (it shows my CMS' error page).

What am I doing wrong?

UPDATE

Wrapping my php in an isset statement, as suggested in the comments:

<?php 
  if (isset($_GET['mailsend'])) {
    $mailsend = $_GET['mailsend'];
    if ($mailsend == 1){
        echo "<div><p>Your e-mail was sent.</p></div>";
    }
  }
?>

The page no longer throws an error, but also never displays the confirmation, even if the URL is contains mailsend=1

JoSch
  • 869
  • 1
  • 15
  • 35
  • 2
    You're probably getting a hidden warning about undefined index. Use `isset` to check the key exists rather than simply using it. If it is set, you can just set it to 1 since you don't expect a value. E.g. `if (isset($_GET['mailsend']))`. – Jonnix Nov 29 '18 at 20:37
  • @JonStirling Thanks for the fast reply. I tried wrapping everything in an isset statement; now the page no longer throws an error but the confirmation also isn't displayed. See the update in the question. Any mistakes I made there? – JoSch Nov 29 '18 at 20:46
  • 1
    I didn't say wrap it, I meant for you to replace it. Get rid of everything within the new `if` block aside from the `echo`. – Jonnix Nov 29 '18 at 20:48
  • ahhh I see, thanks :) Works just fine. You wanna put it in an official answer so I can give you an upvote? – JoSch Nov 29 '18 at 20:50
  • I can't as the question has been marked as a duplicate, but it's fine as long as you're sorted. – Jonnix Nov 29 '18 at 20:51
  • alright, thanks again though – JoSch Nov 29 '18 at 20:52

0 Answers0