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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

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

Post 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?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

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

Post 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.
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

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

Post by lovelf »

OK. Thanks.
Post Reply