0

I've been having a pretty dumb problem as of late.

I'm using the header function to redirect to a confirmation page. I'm declaring $error = false at the top of the php sendmail page.

Here is the conditional on the sendmail page:

if($error)
{
   header('Location: http://www.url.com/confirmation.php
   error='.$error.'type=cell&desc=' . $message);
}
else {
   header('Location:http://www.url.com/confirmation.phperror='.$error.
   '&type=cell&fname=' . $inputFName .'&lname=' . $inputLName . '&email=' . 
    $inputEmail . '&phnum=' . $inputPhnum . '&model=' . $inputModel . 
   '&color=' . $inputColor . '&desc=' . $inputSummary);
}

The problem I'm getting is, when I look at the URL and the get variables, the error portion is empty. For example, my url will look like: confirmation.php?error=&type=cell&fname=Test&lname=Name&email=test@test.com..... etc...

For some reason the error variable is NOT being passed. What's the problem?

DMort
  • 347
  • 1
  • 2
  • 10
  • your converting boolean to string: http://stackoverflow.com/questions/2795177/how-to-convert-boolean-to-string –  Jun 03 '16 at 03:48
  • `error=` has the meaning of *"the error value is empty"*, which will turn into an empty string value, which is equivalent to `false`. That behaviour is actually on purpose if you read the manual entry on string casting; `false` becomes an empty string because it's trivial and unambiguous to convert that back into `false`. – deceze Jun 03 '16 at 04:48

2 Answers2

2

You could pass another value as a "boolean", or simply use 1 / 0;

i.e

?....error=no

and then

if ($_GET["error"] === "no") {
    ...
} else {
    ...
}

or if you're going to use the number method, remember:

1 == true; 0 == false.

  • I'll try that, i'm sure it will work and i'll mark your question as correct, but WHY does a simple boolean not pass through a url? I just find it kind of odd. – DMort Jun 03 '16 at 03:24
  • it's just nice to use if($error) rather than if($error == 1) – DMort Jun 03 '16 at 03:25
  • I get what you mean, it is. If you posted the variable as `""` im sure it would work. I think its probably passing as undefined - try echo'ing the variable. –  Jun 03 '16 at 03:31
  • it was echoing nothing which was weird. If I modified the URL myself and put a true or false where it should be, everything worked. But changing initializing it with 0 right off the bat and changing anywhere I put $error=true; worked just fine.... weird. I'm glad it's working but still wish I knew why that happens. Lol. Thanks though! Happy coding! – DMort Jun 03 '16 at 03:36
0

From http://php.net/manual/en/language.types.string.php:

A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.

So you can either do the following or what @Tobi suggested to achieve it:

$errorStr = $error ? 'true': 'false';
Ankit Gupta
  • 179
  • 1
  • 9