p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   PHP Databases (http://p2p.wrox.com/forumdisplay.php?f=97)
-   -   PHP & JavaScript variables? (http://p2p.wrox.com/showthread.php?t=35654)

mlewitz November 30th, 2005 10:06 PM

PHP & JavaScript variables?
 
Can PHP and JavaScript write to variables that each can share?

For example, if I define a variable with a JavaScript, can I use that variable with a PHP script? ...and vice-versa?

Thanks!

Mike L

mlewitz November 30th, 2005 10:13 PM

Ooops... Sorry, I accidentally posted to the wrong heading.


richard.york November 30th, 2005 10:16 PM

Not exactly. You have to keep in mind that PHP runs on the server, and JavaScript on the client-side. So sharing may not happen in the way you may expect. JavaScript can only send variables to PHP via POST (as in a form submission), GET (form submission, or URL query string), or COOKIE. PHP can create and output JavaScript, so you would merely generate JavaScript as you would HTML to send PHP variables to JavaScript. For example:

Code:

<?php

$hello = 'Hello, world!';

echo "<script type='text/javascript'>\n".
     "    var \$hello = '{$hello}';\n".
     "    alert(\$hello);\n".
     "</script>\n";

?>

HTH!

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design


All times are GMT -4. The time now is 09:02 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.