Page 1 of 1

Browser detection with JS to set a php variable value error.

Posted: Wed Nov 19, 2008 1:59 am
by lovelf
This works:

Code: Select all

<script type="text/javascript">
if (window.opera)
{
document.write('opera');
}
else
{
document.write('different browser'); 
}
</script>
But this does not:

Code: Select all

<script type="text/javascript">
if (window.opera)
{
document.write('<?php $altc="yes"; ?>');
}
else
{
document.write('<?php $altc=""; ?>'); 
}
</script>
 
PHP:

Code: Select all

<?php
if ($altc==yes) {
echo "opera";
}
else {
echo "different";
} ?>
On any browser including Opera with this other code I get "different" on screen, "$altc" gets overwritten.

What could be the problem?

Re: Browser detection with JS to set a php variable value error.

Posted: Wed Nov 19, 2008 2:55 am
by Mark Baker
PHP is executed on the server before the page is sent to the browser.
Javascript is executed within the browser once it has received the page.

Learn the difference between the two.
Programming languages like these aren't pix and mix.

Re: Browser detection with JS to set a php variable value error.

Posted: Wed Nov 19, 2008 2:56 am
by lovelf
OK. Thanks.