How do I pass variables and data from PHP to JavaScript

0 votes

I have a variable in PHP, and I need its value in my JavaScript code. How can I get my variable from PHP to JavaScript?

I have code that looks like this:

<?php
     ...
     $val = $myService->getValue(); // Makes an API and database call
?>

I have JavaScript code that needs val and looks along the lines of:

<script>
    myPlugin.start($val); // I tried this, but it didn't work
    <?php myPlugin.start($val); ?> // This didn't work either
    myPlugin.start(<?=$val?> // This works sometimes, but sometimes it fails
</script>
Jul 6, 2020 in Java-Script by kartik
• 37,520 points
1,872 views

1 answer to this question.

0 votes

Hello @kartik,

Simply use one of the following methods.

<script type="text/javascript">
var js_variable  = '<?php echo $php_variable;?>';
<script>

OR

<script type="text/javascript">
    var js_variable = <?php echo json_encode($php_variable); ?>; 
</script>

Hope it helps!!

Thank you!

answered Jul 6, 2020 by Niroj
• 82,840 points

Related Questions In Java-Script

0 votes
1 answer

How can I pass PHP objects to javascript?

Hello @kartik, You can convert the PHP object ...READ MORE

answered Jul 7, 2020 in Java-Script by Niroj
• 82,840 points
5,732 views
0 votes
1 answer

How to access PHP session variables from jQuery function in a .js file?

Hello, You can produce the javascript file via ...READ MORE

answered Apr 29, 2020 in Java-Script by Niroj
• 82,840 points
12,999 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,840 points
3,278 views
0 votes
1 answer

How do I remove a property from a JavaScript object?

Objects in JavaScript can be thought of ...READ MORE

answered Jun 8, 2020 in Java-Script by Niroj
• 82,840 points
1,492 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,080 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
3,016 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,948 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,887 views
+1 vote
1 answer

How do I pass JavaScript variables to PHP?

Hello @kartik, You cannot pass variable values from ...READ MORE

answered Jul 6, 2020 in Java-Script by Niroj
• 82,840 points
19,706 views
0 votes
1 answer

How to send JSON data from Javascript to PHP?

Hello @kartik, PHP has a built in function ...READ MORE

answered Jul 6, 2020 in Java-Script by Niroj
• 82,840 points
6,961 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP