How can I pass variables and data from PHP to JavaScript?

Ask a question
0

I have a variable in PHP, and I need to send its value to JavaScript. How can I do that?

My code is looks like this:

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

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

<script>
// I tried this, but it didn’t work
.start($val); 

 // This didn’t work either
<?php myPlugin.start($val); ?>

// This works sometimes, but sometimes it fails
.start(<?=$val?> 
</script>
Coin iconoffer bounty
add comment

0 Answer

Your Answer