How to get php variable value in javascript

Possible Duplicate:
What's the best way to pass a PHP variable to Javascript?

I am using the following code:


The value doesn't alert. What is the mistake?

asked May 5, 2011 at 10:01

1

Essentially:




answered May 5, 2011 at 10:07

4

The most secure way (in terms of special character and data type handling) is using json_encode():

var spge = ;

answered May 5, 2011 at 10:06

Stefan GehrigStefan Gehrig

81.4k24 gold badges154 silver badges185 bronze badges

Put quotes around the to make sure Javascript accepts it as a string, also consider escaping.

answered May 5, 2011 at 10:06

DunhamzzzDunhamzzz

14.4k3 gold badges48 silver badges74 bronze badges

**var spge = '';** 
alert(spge);

answered May 5, 2011 at 10:03

Sai SherlekarSai Sherlekar

1,7821 gold badge16 silver badges17 bronze badges

0

Not the answer you're looking for? Browse other questions tagged php javascript html or ask your own question.

  1. HowTo
  2. PHP Howtos
  3. Pass Variable From PHP to JavaScript

Created: May-01, 2021

  1. Use AJAX to Pass the PHP Variable to JavaScript
  2. Use JavaScript to Escape the PHP Scripts to Pass the PHP Variable to JavaScript
  3. Use the Short PHP Echo Tag, Inside the JavaScript to Pass the PHP Variable to JavaScript

We will introduce a way to pass the PHP variable to JavaScript using AJAX. This method requests the data from the PHP server and runs on the client machine.

We will also demonstrate another method to pass the PHP variable in JavaScript by escaping the PHP to JavaScript. We write JavaScript after the PHP scripts to escape the PHP.

We will show you another example to pass the PHP variable to JavaScript using the short echo tag in the JavaScript as where $var is the PHP variable.

Use AJAX to Pass the PHP Variable to JavaScript

We can use AJAX to get the data and variables from the PHP server to JavaScript. This method has separate server-side and client-side scripts. It makes the code cleaner and enhances the code readability. AJAX stands for Asynchronous JavaScript and XML. It uses the XMLHttpRequest object to request the data from the server. It displays the data using JavaScript and HTML DOM. This process occurs asynchronously, which means the changes happen in the webpage without the page being reloaded. However, there is latency when AJAX is used over networks because it sends the HTTP request. In this method, we write JavaScript code in the PHP file.

For example, create a PHP file named index.php. Write the JavaScript code inside the

# php 7.*

Output:

"Mountain"

Use JavaScript to Escape the PHP Scripts to Pass the PHP Variable to JavaScript

We can use JavaScript after the PHP tags to escape the PHP scripts. We can create a PHP variable in the PH P script. Then, we can write the JavaScript to escape the above-written PHP and pass the variable inside the JavaScript code. Inside the JavaScript, we can use the PHP tag to echo the PHP variable assigning it to a JavaScript variable.

For example, assign a value Metallica to a variable $var inside the PHP tags. Write a script tag and inside it, write a PHP tag. Inside the PHP tag, echo a JavaScript variable jsvar. Assign the PHP variable $var to jsvar. Do not forget a quote wrapped around the $var and a semicolon after it. Close the PHP tag and log the value of jsvar using the console.log() function. Close the script tag. In the example below, we have logged the PHP variable $var in the console using JavaScript. We can see the output in the console of the webpage.

Example Code:

#php 7.x


Output:

Metallica

Use the Short PHP Echo Tag, Inside the JavaScript to Pass the PHP Variable to JavaScript

We can use the short PHP echo tag inside the JavaScript to pass the PHP variable to JavaScript. We can use the short PHP echo tag to get rid of the hassle of writing the PHP tag and the echo statement. It is a short-hand method. This method is very much similar to the second method in terms of the concept and the execution.

For example, assign a value Megadeth to a variable $var inside the PHP tags. Open a script tag and create a JavaScript variable jsvar. Write the short-hand echo tag as the value of the jsvar variable. Log the variable using the console.log() function. We can view the output of the script in the console of the webpage.

Code Example:

#php 7.x


?>

Output:

Megadeth

Write for us

DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

How to get php variable value in javascript

Can I access PHP variable in JavaScript?

We can use the short PHP echo tag inside the JavaScript to pass the PHP variable to JavaScript.

How can I pass PHP variable to JavaScript?

We can pass data from PHP to JavaScript in two ways depending on the situation. First, we can pass the data using the simple assignment operator if we want to perform the operation on the same page. Else we can pass data from PHP to JavaScript using Cookies. Cookie work in client-side.

How do you echo a variable in JavaScript?

The echo keyword is used to display the values of variables var1 and var2 to the HTML document which is readable by the browser normally. This property can be taken into an advantage in writing to JavaScript console using PHP. The JavaScript code can be written in the echo section and taken to the HTML document.

What is $$ variable in PHP?

The $var_name is a normal variable used to store a value. It can store any value like integer, float, char, string etc. On the other hand, the $$var_name is known as reference variable where $var_name is a normal variable. The $$var_name used to refer to the variable with the name as value of the variable $var_name.