Am working on an PHP application that involves multiple plans a user can select. On my Manage Plan page, whenever user selects any plan from a menu a ajax call is sent that calculates plan fee based on database values and displays it to the user. That works fine. My only problem is when Javascript is disabled everything gets messed up. Not only the plan fee doesn't show up (which is obvious) but when user clicks Submit button, the plan gets changed (ideally user should be taken to a checkout page)
Anyways, I tried putting something like this at the top of the page.
<noscript>
Javascript is disabled. Please enable it to continue.
<?php exit(); ?>
</noscript>
So when Javascript is disabled it shows a message and stops right there. But when Javascript is enabled the PHP code in <noscript>
tag too gets exceuted and stops the page from running.
I am wondering when Javascript is enabled why everything inside the tag isn't ignored?
Am working on an PHP application that involves multiple plans a user can select. On my Manage Plan page, whenever user selects any plan from a menu a ajax call is sent that calculates plan fee based on database values and displays it to the user. That works fine. My only problem is when Javascript is disabled everything gets messed up. Not only the plan fee doesn't show up (which is obvious) but when user clicks Submit button, the plan gets changed (ideally user should be taken to a checkout page)
Anyways, I tried putting something like this at the top of the page.
<noscript>
Javascript is disabled. Please enable it to continue.
<?php exit(); ?>
</noscript>
So when Javascript is disabled it shows a message and stops right there. But when Javascript is enabled the PHP code in <noscript>
tag too gets exceuted and stops the page from running.
I am wondering when Javascript is enabled why everything inside the tag isn't ignored?
Share Improve this question asked Mar 5, 2012 at 8:31 skosskos 4,2428 gold badges38 silver badges59 bronze badges 4- 8 Don't feel bad. You're like the fifteen billionth person to confuse how PHP and JavaScript work, so you're in good pany. – Ignacio Vazquez-Abrams Commented Mar 5, 2012 at 8:35
- Phew!! I guess then I need to better format my PHP code that can work even in the absence of Javascript – skos Commented Mar 5, 2012 at 8:46
-
@Sachyn PHP will work regardless of javascript; it will work always, and before any javascript/html is even taken into consideration.
better format my php that can work even in the absence of javascript
doesn't make sense – Damien Pirsy Commented Mar 5, 2012 at 8:52 -
@Damien Pirsy I have understood now that PHP is going to run regardless of javascript. So naturally I'll look into my PHP code to make sure system doesn't get changed if client sends invalid data. So do I have any other option than to
better format my php that can work even in the absence of javascript
– skos Commented Mar 5, 2012 at 9:14
9 Answers
Reset to default 7PHP is rendered on the server, not on the client. the <noscript>
is only regarded or disregarded by the browser. the server has no knowledge what the client will do with this tag.
even HTML ments containing PHP will be executed by the page.
Because the page is generated in the first place on server-side and then sent to the browser in the second place. :) In addition the server doesn't know about the <noscript>
-tag.
PHP gets executed server-side, not on the client. Your server doesn't know anything about JavaScript, because JavaScript is executed client-side.
You can't string php and js like that. php runs on your server, js runs on the clients' browsers, therefore exit()
is always called.
The noscript tag is only interpreted by the browser rendering engine and javascript, and not by PHP on the server-side - so what is happening here is that the server is processing the PHP file before sending it to the client (the server, at this stage, doesn't care about the meaning of HTML tags) It hits exit(); and halts execution.
What is then generated is sent to the browser and displayed accordingly. This will be a noscript tag, the text "Javascript is disabled. Please enable it to continue." and then it hits the end of what PHP produced.
I'm not sure what might be happening with your original situation though, sounds like there may be some similar confusion with the placement of PHP within noscript tags. Can you post an example of the original script?
The PHP code is (almost) always executed, no matter where it is in the page. Your problem is not that simple, since the <script>
tag is client-side code, while PHP is server - side code.
Server - side code is executed in the, well, server. In contrast, the client - side code is executed in the client's machine. So PHP is not really aware of the <script>
tag or whether the client can execute scripts at all. It just notices <?php ... ?>
sections and works on those, before sending the result to the client.
Your <?php exit(); ?>
executes on the server side before the page is served to the user. PHP does not care if it's in a <noscript>
tag or not, therefore all further action will stop right before your </noscript>
tag.
A possible solution is adding a disabled="disabled"
attribute to your submit button, and use Javascript to remove the attribute on page load. That way all users will see the whole form, but non-Javascript users will not be able to actually click the button (remember to still notify them in the <noscript>
element.
Because PHP is executed on the server, before the browser even sees the <noscript>
tag. Javascript is client side, PHP is server side.
The why has been answered.
But a solution to call a php file, I found this post.
Will depend on the context of your need. worked for me.
<noscript><iframe src="your_php_file.php"></iframe></noscript>
Link: including php in <noscript> tag