Trying to test the # of users if javascript is turned on. I thought this trick would work, but forgot that the php executes either way.

<script>
<?php
    $sql_insert = "INSERT IGNORE into `test_javascript` (`javascript`,`dateTime`) VALUES ('1',now())";          
    mysql_query($sql_insert,$link) or die("Insertion Failed:" . mysql_error());
?>
</script>
<noscript>
<?php
    $sql_insert = "INSERT IGNORE into `test_javascript` (`javascript`,`dateTime`) VALUES ('0',now())";          
    mysql_query($sql_insert,$link) or die("Insertion Failed:" . mysql_error()); 
?>
</noscript>

Any ideas on how to fix this script or other ways to find % of users with javascript turned on.

60% of our users use IE, and a few of them (that we know of), don't have javascript enabled, so I'd like to get a better sense of the number of users without javascript.

Comments

That's not how <noscript> works. <script>/<noscript> is entirely browser-side.

Written by ceejayoz

That is funny though. Like <a onclick="<?php update_database(); ?>"> or <noscript>alert("You must enable javascript!");</noscript> :)

Written by Wesley Murch

Madmartigan... It's the future :-)

Written by Bob Cavezza

Accepted Answer

When your page loads, you can insert an entry into a database in PHP. This will give you the total number of page requests.

In Javascript, you can initiate another request using AJAX. This would post to a PHP script, but requires Javascript to work properly. This query would tell you the number of requests of browsers with Javascript support.

If you log timestamps and IP addresses (or any other state variables), you should be able to correlate the entries between the two insertions.

Or, another method would be to have the AJAX call update the row inserted by the PHP query. This would be a cleaner approach in my opinion.

Written by Zach Rattner
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki