Value Exchange from Javascript/HTML page to PHP and from PHP page to Javascript

I have re-written this post Passing values from PHP, Javascript,..: Please check if you find that more useful. Thanks!

After reading the post, you will get many different methods for passing and sharing value from one language to another or from server to client and vice-versa. Some of the question asked like how to pass PHP value to CSS. Here if you read carefully then you see that I am passing PHP content to JavaScript. Therefore, that way you can pass PHP value to CSS too. Just read carefully and try to understand it. You will get lot of other methods combining these methods.
Also, the scripts mentioned below are not very useful to be used directly. It is just a sample for your understanding.
When we have started learning PHP, we all face these problem which are not very logical questions, but it comes into our mind. This is how we all grow!

JavaScript call PHP scripts

When you submit a form using JavaScript to a PHP page you can say JavaScript called PHP.

If you write something like this:

1. <script src="mailNew.php?sub=Subject&body=mail body" type="text/javascript" ></script>,
then PHP scripts will be executed.
2. <img src="mailNew.php?sub=Subject&body=mail body" width="1" height="1">Call</img>

Here in both case you can not get any visible output in calling page. You can use these methods for passing value to another host, setting global variables, getting database value and setting global variables. PHP scripts will be parsed.
If you have written PHP scripts for sending mail in mailNew.php file using "sub" and "body" variables then mail will be fired.

Simply you can check the method by setting a session variable in mailNew.php and getting the value of that session value after refreshing the calling page.

Using Ajax: Ajax is also a JavaScript code. You can use Ajax for calling PHP pages.


Passing javascript variable's value to php/How to use JavaScript Variable in php

Directly you cannot do this. But when you pass a value to a url like:

1. <a href="phpPage.php?sub=mail Subject">Call PHP page</a>
then $_GET['sub'] auto global variable
will be set.

2. If you are really desperate for passing JavaScript variable value to PHP then:

var mySite = '';
// Set Javascript value to pass
function setmysite(v1) {
if (v1.options[v1.selectedIndex].value == '#') return;
if (v1 == 'tech')
mySite = '';
if (v1 == 'general')
mySite = '';
function submitFun(f1) {
// will pass javascript value to php ($_GET).
//(example of submit page using JavaScript and php/ Javascript form submission)
t = './submitTo.php?s=' + mySite;
t = encodeURI (t); // encode URL
// assign form's action a new value. Original "action" value will be overwritten.
f1.action = t;
f1.submit(); // submit form using javascript
return true;
<form method="post" action="" onsubmit="submitFun(this)">
<select name="site" size="1" onchange="javascript:setmysite(this);">
<option value="#">--Select--</option>
<option value="tech">Tech</option>
<option value="general">General</option>
<input type="submit" />

This is a sample case and may be not a real issue for you.
Here you are sending javascript variable "mySite" to PHP.
So, this way you are getting javascript variable in PHP.

On submitTo.php page you can get javascript variable 'mySite' value in $_GET['s'];

echo $_GET['s']; // variables (mySite) from javascript to php

3. Cookie is another good method for exchanging PHP - Javascript variable.

You can set cookie by PHP and get that cookie value by javascript and set cookie by javascript and get that value using php also. Use setcookie for setting cookie and $_COOKIE[] for getting cookie set by either PHP or Javascript.
More on PHP and Js Cookie

How to use PHP Variable in Javascript

This work is easy. PHP is parsed before javascript, so if you want to send PHP value
to Javascript, then

var jsVar = <?php echo $phpVar; ?> // pass PHP value to Javascript

So, if your php variable "phpVar" has some value then it will be readily accessible to current page.

PHP function from JavaScript

It is same as "Javascript call PHP scripts". Use a function's body in called page.
Like <a href="phppage.php?arg=value">Call PHP function</a>
In phppage.php:-

function urFunction()
if ($_GET['arg'] == 'value') {

PHP Array to Javascript and PHP echo JavaScript

$phpArray1 = array('a', 'b', 'c');
$outputJavascript =  '<script>';
$outputJavascript .= ' var jsArray = new array('.implode(',',$phpArray1). '); ';
$outputJavascript .= ' </script> ';
echo $outputJavascript;
<script type="text/javascript">
// Creating Array Directly in Javascript
jsArray2 = new Array();
jsArray2[0] = 'x';
jsArray2[1] = 'y';
alert(typeof jsArray2);
alert(typeof jsArray);  // Checking type

PHP Code in Javascript

If it is same as PHP var in Javascript then OK else please change your requirement.:)

PHP and JS variables in HTML

Js variable value in HTML
jsValue = 'Web 2.0';
document.write ('<input type=text value="' + jsValue + '" name=txtField />');
PHP var. value in HTML
As you have assumed
$phpValue = 'Web 2.0';
<input type=text value=<?=$phpValue?> name=txtField />

Pass a PHP variable from one page to another (in PHP)

(Get data from one page to another page in php)

1. If you are getting data from user through form (<form>), all input data will be automatically available to the page you mention in action attribute (<form action=”targetpage.php”>).
You will get all data from $_POST or $_GET array.
2. If you want your data shared among many pages then you can use $_SESSION, $_COOKIE, file or table. The data can be mysql retrieved data or user given data. For example:- you may like to show user name on every page.

Using session –

$_SESSION['userName'] = ‘Dear PHP MySQL’;
On next page/another page, retrieve data like this:
echo $_SESSION['userName'];
// unset ($_SESSION['userName']); // delete if not required further

Using cookie

Setcookie ('userName', 'Dear PHP MySQL');
//On another page or on same page after this visit
echo $_COOKIE['userName'];
// setcookie('userName', ''); // delete if not used further

Using file for passing data in various session:

$str = 'Dear PHP MySQL';
$hn = fopen ('filepath/fileName.php', 'w+');
if ($hn) {
fwrite($hn, $str);
fclose ($hn);

On any page and in any session:

// get file data
$hn = fopen ('filepath/fileName.php', 'r');
if ($hn) {
$str = fread($hn, filesize($filename));
echo $str;
fclose ($hn);

You will use "global" for getting variable value in function, if the variable is defined before calling the function on the same page.

If you want to get and share data in another session or between different users then you will need to use file read/write method or you have to use table as PHP don’t have application variable like in .NET.

  • # 1 - by Neil Murphy McLaughlin

    These are excellent representations for people new to web programming.Thanks!

  • # 2 - by free style

    very useful!

  • # 3 - by free style

    very useful!

Comment pages
1 2 193
Comments are open for an year period. Please, write here on Facebook page.