Organizational Research By

Surprising Reserch Topic

get variable from php to javascript

get variable from php to javascript  using -'I,want,to,use,a,PHP,variable,in,JavaScript.,How,is,it,possible?'

asked Sep 8, 2015 by rajesh
edited Nov 18, 2015 by rajesh
0 votes

Related Hot Questions

6 Answers

0 votes

You can pass PHP Variables to your JavaScript by generating it with PHP:

answered Sep 8, 2015 by rajesh
0 votes

You can print the PHP variable into your javascript while your page is created.

Of course this is for simple variables and not objects.

answered Sep 8, 2015 by rajesh
0 votes

I think the easiest route is to include the jQuery javascript library in your webpages, then use JSON as format to pass data between the two.

In your HTML pages, you can request data from the PHP scripts like this:

$.getJSON('http://foo/bar.php', {'num1': 12, 'num2': 27}, function(e) {
    alert('Result from PHP: ' + e.result);

In bar.php you can do this:

$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
echo json_encode(array("result" => $num1 * $num2));

This is what's usually called AJAX, and it is useful to give web pages a more dynamic and desktop-like feel (you don't have to refresh the entire page to communicate with PHP).

Other techniques are simpler. As others have suggested, you can simply generate the variable data from your PHP script:

$foo = 123;
echo "\n";

Most web pages nowadays use a combination of the two.

answered Sep 8, 2015 by rajesh
0 votes

It depends on what type of PHP variable you want to use in Javascript. For example, entire PHP objects with class methods cannot be used in Javascript. You can, however, use the built-in PHP JSON (JavaScript Object Notation) functions to convert simple PHP variables into JSON representations. For more information, please read the following links:

You can generate the JSON representation of your PHP variable and then print it into your Javascript code when the page loads. For example:

answered Sep 8, 2015 by rajesh
0 votes

Sure, there are ways to get the $_GET object in javascript by passing it directly from php, but there are other ways ;D

I will proceed to explain both.

1) Direct parse by function. This just grabs the url and parses it directly with RegEx

        return RegExp('[?&;]'+key+'=([^?&#;]*)').exec(location.href)[1]
        return def||''

Easy peasy, if the query string is ?ducksays=quack&bearsays=growl, then $_GET('ducksays') should return quack and $_GET('bearsays') should return growl

Now you probably instantly notice that the syntax is different as a result of being a function. Instead of $_GET[key], it is $_GET(key). Well, I thought of that :)

Here comes the second method:

2) Object Build by Loop

    $_GET={}//the lack of 'var' makes this global'&')//not '?', this will be dealt with later
    for(i in str){

Behold! $_GET is now an object containing an index of every object in the url, so now this is possible:

$_GET['ducksays']//returns 'quack'

AND this is possible

for(i in $_GET){
    document.write(i+': '+$_GET[i]+'
') }

This is definitely not possible with the function.

I hope this was helpful and interesting

answered Sep 8, 2015 by rajesh
0 votes

This is the easiest way of passing a php variable to javascript without Ajax.

You can also use something like this:

var i = "";

This said to be safer or more secure. i think

answered Sep 8, 2015 by rajesh