Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP FAQs
PHP FAQs This moderated forum is where our PHP experts answer some of the questions they see asked most frequently in the other PHP forums. You cannot post other messages here, use the other PHP forums for that.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP FAQs 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 April 8th, 2004, 11:34 AM
Friend of Wrox
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default Q. How do I pass PHP variables to JavaScript?

JavaScript cannot actively read variables from the server, but when a page loads, PHP can pass variables to JavaScript by writing to the page the JavaScript is on.


    $username = "Snib";

<script language="javascript" type="text/javascript">
    var sStr = "My name is <?php echo $username ?>.";

    This code will output this text:
    My name is Snib.
By editing the JavaScript on the page, PHP can set JavaScript variables.

This is very useful for passing database values to JavaScript to compare with a JavaScript variable or number. Let's say that there is a MySQL database called db1, with a table called table1, which has two columns, 'userid' and 'usernum'. To retrieve information about Snib, you can use code like this:


    $connection = mysql_connect('localhost', 'username', 'password');

    $sql = mysql_select_db('db1', $connection);

    $userid = "Snib";

    $query = "SELECT * FROM `table1` WHERE userid='$userid'";

    $result = mysql_query($query);

    $data = mysql_fetch_assoc($result);

    $usernum = $data['usernum'];


<script language="javascript" type="text/javascript">

    var userid = "<?php echo $userid; ?>";

    var usernum = "<?php echo $usernum; ?>";

    //if usernum is less than or equal to 50
    if (usernum <= 50) { 

        alert("You are not allowed on this page, " + userid);
        setTimeout('window.location = "otherpage.php"', 3000); // user leaves in 3 seconds

    } else {

        alert('Welcome to this page, ' + userid);



<?php mysql_close($connection); ?>
Although setting JavaScript variables with PHP is helpful, it cannot always be done. For instance, PHP cannot modify external .js documents temporarily. It changes them for good.


MySQL with PHP: http://php.net/mysql
JavaScript: http://javascript.com http://msdn.microsoft.com/library/default.asp (JScript and DHTML)


P2P Member
-Snib - http://www.snibworks.com
Where will you be in 100 years?

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP & JavaScript variables? mlewitz Beginning PHP 2 December 2nd, 2005 08:18 AM
PHP & JavaScript variables? mlewitz PHP Databases 2 November 30th, 2005 10:16 PM
Passing PHP variables to javascript wfrisch Pro PHP 10 April 5th, 2005 05:13 AM
passin javascript variables to another page in php ismnanie Javascript 0 January 26th, 2005 01:24 AM
How Do I Pass 2 Variables in Javascript Lucy Javascript 2 December 13th, 2003 11:25 AM

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