Passing value from one to another considering PHP, HTML, JavaScript

This post is a little beautified article from earlier passing variables and values.

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

Again, 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:

// I will pass this variable value to php
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 submitFunction(f1) {
// Will pass javascript variable mySite’s value to php $_GET variable.
frmAction = './submitTo.php?webSite=' + mySite;
frmAction = encodeURI (frmAction); // encode URL
// assign form's action a new value. Original "action" value will be overwritten.
f1.action = frmAction;
f1.submit(); // submit form using javascript
return true;
<form method="post" action="" onsubmit=" submitFunction(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. Use your thinking for using this in your work.
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['webSite '];


Cookie is another good method for exchanging value of variables between PHP and Javascript.

You can set cookie by PHP and get that cookie value by javascript and set cookie by javascript and get that variable’s value using php also. Use PHP setcookie for setting cookie and $_COOKIE[] for getting cookie value set by either PHP or Javascript. (Managing Cookie using PHP and Javascript)

JavaScript calls PHP scripts

Directly you cannot do this. But, when you submit a form using JavaScript to a PHP page you can say JavaScript called PHP. See the above js form Submit function.

Here I am discussing different tricks.

If you write something like this:

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

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

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

How to use PHP Variable in JavaScript:

This is an easy work. PHP is parsed on server and generated HTML is sent over to browser. So if you want to send PHP value to JavaScript, then

var jsVar = <?php echo $phpVar; ?> // passing PHP variable’s value to Javascript

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

Call PHP function from JavaScript

It is same as "Javascript calls 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 phpFunction()
if ($_GET['arg'] == 'value') {

or Use AJAX for doing this. Then you can call the php page without loading the page. This action can be fired using lots of Javascript events like onchange, onclick, onmouseup and so on.

PHP echo JavaScript code

With the above solution you will also learn about how to send PHP Array to 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 variable 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 />
<span><?php echo $phpValue; ?>

This was so easy!

One better way to pass PHP values to HTML is Ajax. Learn Ajax! It is simple. Ajax will get the PHP values from the Server and return you the generated HTML from PHP code. Whatever will be in echo/print etc and/or those text outside of the php will be available to you from Ajax without page reload or submit.


Comments are open for an year period. Please, write here on Facebook page.