-2

I am fundamentally confused about something in my code.

If I code this on one of my .php pages:

$userEmail = $_SESSION["inputEmail"];

and then use it in an AJAX call inside one of my Javascript functions:

function firstFunction() {
    $.ajax({
        url: someUrl.php?inputEmail='<?php echo $userEmail ?'>
        sync: true,
        success: function(result) {
          $("#someDiv").html(result)
         }
      })
}

I end up getting the result I need. I am confused, however, because if in this function, I did something like this:

function secondFunction() {
    if (<?php echo $userEmail ?> == "test@test.com") {
        //do some stuff
    }
}

It does not work. I have read answers to this exact scenario right above, but I don't understand why the first line of code I provided in this example works but this second one does not.

Initially, I expected the Javascript to be able to read $userEmail in the function above, but after viewing the page source, I see it is evaluation nothing. It looks like:

if(  == "test@test.com)

Like I said, I've looked this part up. But now I'm even more confused. Why can I do the AJAX call under firstFunction() but not secondFunction()

brennvo
  • 89
  • 1
  • 7
  • 1
    Is that function in a separate file? – Qirel Dec 11 '17 at 06:32
  • 3
    simple quotes issue right? `if ("" == "test@test.com") {` – Lawrence Cherone Dec 11 '17 at 06:35
  • I'm the same opinion like @LawrenceCherone because `echo $userEmail` is an string ouput which has to be surrounded by quotes like `"test@test.com"` – julianstark999 Dec 11 '17 at 06:37
  • seems like an odd way to check for a 1 in a gazillion existing email address :/ – Lawrence Cherone Dec 11 '17 at 06:37
  • Or the PHP variable is not defined where you attempt to print it. Lawrence is right though, the resulting string needs to be quoted in javascript. – Qirel Dec 11 '17 at 06:38
  • This was the issue I was having with the `if()` statement I gave: https://stackoverflow.com/questions/8379058/using-php-in-javascripts-if-statement/8379070 – brennvo Dec 11 '17 at 06:41
  • Your fundamental confusion is: Even if that javascript code does what you expect it to do, javascript is **not** able to **read** `$userEmail`. You simply use php to assemble a string, that your browser will interpret as js code and run it. Php runs on your server, js in the browser. There is no such thing as "one technology reads values of the other". – simon.ro Dec 11 '17 at 06:55
  • I would avoid doing this whenever possible -- Javascript has AJAX / Fetch requests that you can make to get data from the server-side. – Jhecht Dec 11 '17 at 07:07

1 Answers1

0

The reason why your ajax is running correctly because you php code is
inside a qoute '<?php echo $userEmail; ?>'.

Try this

function secondFunction() {
    var inputEmail = '<?php echo $userEmail; ?>';
    if (inputEmail == "test@test.com") {
        alert(inputEmail);
    }else{
        alert(inputEmail);
    }
}


Just like what Francis Avila said in the link(Using PHP in javascript's IF Statement) you've given

"PHP is evaluated on the server before the page is sent to your browser. By the time the browser sees it and executes the javascript, all the PHP is gone."

Rex Martinus
  • 155
  • 10