simple echo

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
JonathanStanton
Forum Newbie
Posts: 10
Joined: Wed Jul 21, 2010 10:07 am

simple echo

Post by JonathanStanton »

Why doesn't this code work?

<script type="text/javascript">
width = screen.width;
alert('<?php echo $_GET['width']; ?>');
</script>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: simple echo

Post by superdezign »

That depends. What are you trying to do? What are you expecting this code to accomplish?
JonathanStanton
Forum Newbie
Posts: 10
Joined: Wed Jul 21, 2010 10:07 am

Re: simple echo

Post by JonathanStanton »

the Javascript variable is a JSON request so in actuality it's

width = data.lastID;
alert(width);
alert('<?php echo $_GET['width']; ?>');

the first Alert returns my variable correctly
but I really need it do be echo through PHP because I'm putting it through some jQuery stuff and it wont work with js variables needs to be echo'ed
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: simple echo

Post by superdezign »

Again, not exactly sure what you are trying to accomplish...

However, it seems as if you are trying to access data that PHP does not have, or trying to send data to PHP. If you do not have "width" defined in the query string of your URL, then $_GET['width'] will be empty. If you are trying to send this width to PHP, then you are out of luck. PHP is server-side, and thus is processed well before JavaScript (which is client-side) is. The only way for JavaScript to communicate with PHP is through AJAX, and it's not exactly a simple process.
JonathanStanton
Forum Newbie
Posts: 10
Joined: Wed Jul 21, 2010 10:07 am

Re: simple echo

Post by JonathanStanton »

oh I understand now, thank you :)
Post Reply