Basics
Creating a Document
Head and Body Elements
Formatting Text
Creating Lists
Embedding Images
URLs Explained
Linking to Documents
Creating Tables
Forms Form Basics
Input and Textarea
Select Lists
Frames Framesets
The Frame Element
Nested Framesets
Targetting Frames
Inline Frames
Tips and Tricks Meta Tags
Transition Effects
HTML Generators Create a Document
Create a List
Create a Table
HTML
Basics
Implementing CSS
CSS Syntax
Pseudo Classes/Elements
CSS Classes
CSS Properties Font Properties
Color and Background
Text Properties
Border Properties
Margins and Padding
Size and Position
Tips and Tricks Menu Buttons
Special Effects
CSS
Basics
Running a Script
Variables
Expressions and Operators
Objects.Properties.Methods
The Date Object
Strings
Regular Expressions
Defining RegExp Patterns
Branches and Conditions
Loops
Arrays Array Basics
Array Methods
Sorting Arrays

User-Defined Functions
Cookies
Windows
Frames
Tips and Tricks Image Replacement
Using Includes
Form Validation
Debugging
JavaScript
Basics
Creating a Script
Running a Script
Variables
Expressions and Operators
Strings Strings Basics
Strings and Substrings
Replacing Substrings
Regular Expressions
Branches and Conditions
Loops
Arrays Array Basics
Array Functions
Sorting Arrays
User-Defined Functions
Include and Require
Uploading Files
File Functions
Session Variables
Tips and Tricks Page Templates
Form Reply Scripts
Form Validation
JavaScript to PHP
PHP
Basics
Create and Drop
Show and Describe
Insert, Update and Delete
Querying
Join Queries
Functions
Table Locking
PHP/MySQL Functions Accessing a Database
Querying with PHP
Create and Drop with PHP
Insert and Update with PHP
Frequently Used Functions MySQL
Basics
Layout and Navigation
Page Content Style
Web Page Copy
Graphics and Animation
HTML Forms
Accessibility
Legal Requirements
MySQL
PHPBasicsCreating a ScriptRunning a ScriptVariablesExpressions and OperatorsStringsStrings and SubstringsReplacing SubstringsRegular ExpressionsBranches and ConditionsLoopsArraysArray FunctionsSorting ArraysUser-Defined FunctionsInclude and RequireUploading FilesFile FunctionsSession Variables
Recommended Reading
PHP and MySQL web development

programming PHP

PHP cookbook

Home > PHP Tips and Tricks > JavaScript variables to PHPprinter version

JavaScript Variables to PHP

It's easy to pass PHP variables to JavaScript but there's no direct way to pass JavaScript variables to a PHP script - but there is an indirect way.

Passing PHP Variables to JavaScript

Since PHP can output plain text, HTML or JavaScript all you need to do is use echo() to print out a string, with PHP variables embedded, inside a JavaScript.

Passing PHP variables to a JavaScript
<html>
<head>
</head>
<body>
<script language="javascript" type="text/javascript">
    <?php
    $name = 'Dave';
    echo('var name = "'.$name.'";');
    ?>
    document.write(name);
</script>
</body>
</html>


Passing JavaScript Variables to a PHP Script

HTML does interface with PHP via CGI so can pass variables from an HTML page to a PHP script by using a query string.

The query string starts with ? and each variable is separated by &amp;.

Passing Variables via CGI Using a Query String
<html>
<head>
</head>
<body>
<a href="another_page.php?name=Dave&amp;age=40">another page</a>
</body>
</html>


Since JavaScript can output HTML using the document.write() method you can construct a link using concatenation to write the JavaScript variables into the query string.

When a user clicks the link the JavaScript variables will be passed to the PHP script addressed by the link.

Passing JavaScript variables to a PHP Script
<html>
<head>
</head>
<body>
<script language="javascript" type="text/javascript">
    var name = "Dave";
    var age = 40;
    document.write('<a href="another_page.php?name='+name+'&amp;age='+age+'">another_page</a>');
</script>
</body>
</html>


Important Points to Note

Query strings can produce unforeseen results if certain characters are used i.e. characters that are reserved as delimiters.

So if you're not sure what value a string variable may take then you should have code in place to check it before it's passed, especially if the string is gathered from user input.

You can't use this method to pass JavaScript variables to a PHP script on the same page as the JavaScript. They have to be passed to a PHP script on another page.

The reason is that PHP is parsed server side before the page is downloaded and JavaScript is parsed client side after the page is downloaded.

The variable values have to be written before they're passed and this wouldn't happen if you tried to write them directly to PHP in the same page, since the PHP would be parsed before the JavaScript.


Privacy | Terms | Contact | Links | Sitemap | RSS Feeds RSS and JavaScript Feeds
©2010 www.webdesignworkmate.co.uk all rights reserved 
Design and Production by smallbizonline website design © 2000-2010
Valid HTML 4.01! Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0Valid CSS!
Tips and TricksPage TemplatesForm Reply ScriptsForm ValidationPassing JavaScript variables to PHPJavaScript to PHP
Got any PHP Tips?
Send me your tip and if it's suitable I'll put it on the site, credit it to you and add a link back to your site.