0

Hoping someone can help me figure out how to dynamically update a php header redirect in my mail handler file using a variable.

What I was using in my PHP mail handler:

header('Location: filename.php');


What I tried to do:

Front End File

$tyTarget = "filename.php";

<input type="hidden" name="redirDestination" value="<?php echo $tyTarget ?>" />

PHP Mail Handler

$thankYou = $_POST['redirDestination'];

header('Location: $thankYou');



Background:

I'm templating out a few landing pages. They use a standard HTML form that passes a couple of hidden inputs that we use internally. Historically, each landing page had its own PHP mail handler which we'd simply use a header redirect on submit to its own thank you page (i.e. thank-you-facebook.php or thank-you-email.php) which gets used for conversion tracking for each source.

Working to consolidate a bunch of these pages and use a few PHP includes to simplify the template but dynamically loading the redirect target from a variable didn't work.

Do I just have a syntax error in my redirect?

Any help is appreciated. PHP noob.

Update per duplicate question flag:
As someone who spent a while looking for answers to this question before posting, flagging this as a duplicate question because the correct answer loosely fell in line with a question regarding single quotes in PHP would not have helped me find the answer. In other words, the question was different and not a duplicate. The solution accepted as the answer just happened to be similar.

N'Bayramberdiyev
  • 5,936
  • 7
  • 27
  • 47
sforcash
  • 429
  • 4
  • 16
  • 1
    `header('Location: $thankYou');` is using single quotes. You would want `header('Location: '. $thankYou);` – IncredibleHat Oct 29 '17 at 20:45
  • 1
    or `header("Location: $thankYou");` –  Oct 29 '17 at 20:46
  • Yeah that works too @nogad! I personally shy away from double quotes with included variables, as php is then trying to interpret the whole string for variables and code. – IncredibleHat Oct 29 '17 at 20:48
  • 1
    @Randall just dont call it "faster" :-) –  Oct 29 '17 at 20:52
  • Right. My reasoning isnt about speed, but more of "oops I put too much in that string and I'm getting unexpected results because php decided my dollar amounts are a variable" ;-) More operator error, than a speed issue haha. – IncredibleHat Oct 29 '17 at 20:58

1 Answers1

2

Judging from your code paste, your issue is a simple syntax goof. You have:

header('Location: $thankYou');

Which is using single quotes around the variable. You would want:

header('Location: '. $thankYou);
IncredibleHat
  • 4,000
  • 4
  • 15
  • 27
  • This absolutely works. I'll give you credit for the answer as soon as the timer is up. I'm not sure I understand the difference between the double and single quotes in this case. I'll go back to the book and see what I come up with, but if you've got any high-level insight you want to share, I'm trying to grasp this more than "hey, that works!" =). – sforcash Oct 29 '17 at 20:52
  • 1
    @sforcash some light reading: http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php –  Oct 29 '17 at 20:53
  • @nogad exactly what I was looking for. Thanks bud. – sforcash Oct 29 '17 at 20:54