Monday, 30 May 2016

Using PHP in javascript's IF Statement




I am trying to make a confirm box which will desire which php code will be executed.
Heres my code:







The problem is , even if i click YES, $confirmation and boolean from defined() function returns 1.
Whatever I click, (cancel or ok) one of them should be 0 (I've already declared $confirmation before)
But both of codes at if and else blocks are used!

Normally it works like this


Answer



You fundamentally misunderstand what PHP is doing.



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.



Use your browser's "view source" on the browser window with this code in it. You'll see it looks like this:







You either need to implement what you want to do in javascript to run on the browser, or you need to send a new request to the server and get a new page back (either directly or indirectly) with your response.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...