Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old November 30th, 2005, 10:06 PM
Registered User
Join Date: Nov 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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?


Mike L
Old November 30th, 2005, 10:13 PM
Registered User
Join Date: Nov 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts

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

Old November 30th, 2005, 10:16 PM
richard.york's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts

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:


$hello = 'Hello, world!';

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



Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP & JavaScript variables? mlewitz Beginning PHP 2 December 2nd, 2005 08:18 AM
Passing PHP variables to javascript wfrisch Pro PHP 10 April 5th, 2005 05:13 AM
Javascript & PHP rajuru Beginning PHP 6 February 22nd, 2005 06:15 AM
passin javascript variables to another page in php ismnanie Javascript 0 January 26th, 2005 01:24 AM
Q. How do I pass PHP variables to JavaScript? Snib PHP FAQs 0 April 8th, 2004 11:34 AM

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