Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP FAQs
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Closed Thread
 
Thread Tools Display Modes
  #1 (permalink)  
Old April 8th, 2004, 11:34 AM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 1 Time in 1 Post
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.

Code:
<?php

    $username = "Snib";

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

    document.write(sStr);
</script>
...
    This code will output this text:
Code:
    ...
    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:

Code:
<?php

    $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);

    }

</script>

<?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.

Reference:

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

Snib

P2P Member
<><
__________________
-Snib - http://www.snibworks.com
Where will you be in 100 years?
Closed Thread


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP & JavaScript variables? mlewitz Beginning PHP 2 December 2nd, 2005 07:18 AM
PHP & JavaScript variables? mlewitz PHP Databases 2 November 30th, 2005 09: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 12:24 AM
How Do I Pass 2 Variables in Javascript Lucy Javascript 2 December 13th, 2003 10:25 AM



All times are GMT -4. The time now is 07:40 PM.


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