2

I have a series of PHP statements that I only want to run if javaScript is enabled.

if($js_enabled == 'yes') {
   //Run a series of PHP statements
}

My problem is that I want to create that $js_enabled variable with javaScript. Therefore, if javaScript is enabled, the variable will be created and the PHP statements will be run, but if javaScript is not enabled, the variable will never be created and the PHP statements will not run.

How can I create a variable with JavaScript that PHP can recognize?

zeckdude
  • 15,877
  • 43
  • 139
  • 187
  • 2
    Why not just emit both outputs and use the ` – kennytm Apr 02 '10 at 08:34
  • 3
    Uh I am not sure you have the right idea of PHP and Javascript. Read this: http://stackoverflow.com/questions/2034501/how-does-php-work – Felix Kling Apr 02 '10 at 08:39
  • @KennyTM - Because any PHP I put within the noscript tag is still run when javascript is enabled. It won't skip over that PHP, just because they're within noscript tags. – zeckdude Apr 02 '10 at 09:20
  • @Felix - That is a very generic answer. Thanks alot for not helping. I understand that one is a client side and one is a server side language. Perhaps you can provide me a suggestion on how I can only make a specific set of PHP statements run if javascript is enabled, other than to say go learn both of the languages? – zeckdude Apr 02 '10 at 09:24
  • Can you elaborate on the code you want to run if Javascript is enabled? It sounds as though your design is a little contrived at this point. You should really send all the data needed to the client and have client side Javascript make the decision to display it or not. Or as mck89 has suggested, use an AJAX call to run the server-side code for JS enabled clients. – Greg K Apr 02 '10 at 09:27
  • 1
    @zeckdude: See Gordon's or mck89's answers. I just commented on your question, not answered it, because I feared that you have a wrong view at it. Sorry for *trying* to help. – Felix Kling Apr 02 '10 at 09:27

5 Answers5

6

You can do

$browser = get_browser();
if ($browser['javascript'] == 1) {
    // your code here
}

Please read the documentation for get_browser for caveats, especially

Note: In order for this to work, your browscap configuration setting in php.ini must point to the correct location of the browscap.ini file on your system. browscap.ini is not bundled with PHP, but you may find an up-to-date » php_browscap.ini file here. While browscap.ini contains information on many browsers, it relies on user updates to keep the database current. The format of the file is fairly self-explanatory.

Also checkout the link given in the comments to get_browser:


EDIT As you correctly point out in the comments, the above will not tell you if JavaScript is enabled but just whether the browser is capable of running it. Finding out about the clientside from the serverside is impossible, because the serverside runs first. If anything, inform the serverside after a page has been served, e.g. like @mck89 suggested or simply set a cookie you can read on each subsequent request.

But generally speaking, if your site requires JavaScript enabled, you should simply add a message to inform the user about this requirement, e.g.:

<noscript>This page requires JavaScript to run properly</noscript>

In other words, don't check from PHP. Just set every variables the browser might use, if JavaScript is enabled and consider using graceful degradation or progressive enhancement.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • From what I read in their documentation, this will only tell me if their browser supports JavaScript, not if they have it disabled. – zeckdude Apr 02 '10 at 10:38
2

Why don't you simply an ajax request? If javascript is disabled the ajax request can't be done so the PHP script will not be executed, if javascript is enabled the ajax request starts the execution of the PHP script.

mck89
  • 18,918
  • 16
  • 89
  • 106
1

You can store that value in a hidden field. and check it on server side.

thelost
  • 6,638
  • 4
  • 28
  • 44
1

If i would need it that badly I would use JavaScript to set a form variable (prefferably hidden) on page before and then check it when processing form.. Otherwise i would make my pages work without JS...

kviksilver
  • 3,824
  • 1
  • 26
  • 27
0

This can't be done. PHP is server side while javascript is interpreted only after the php interpretation has already been done and the code already sits in the user's browser. Try a workaround using ajax. if javascript_enabled -> call with ajax some php page.

nc3b
  • 15,562
  • 5
  • 51
  • 63