In this article will give you an example of how to access php variables in jQuery. I would like to share with you how to use php variable in jQuery. Since PHP is executed on the server and jQuery on the client, you need to pass PHP data to front end via HTML, Javascript, or AJAX. Here are some examples.
If you are working on PHP and you need access to PHP variable inside the jQuery then you can simple get value.
1. How to access PHP variables in JQuery directly
You can directly embed PHP variables in JQuery, which jQuery can then access. This method is simple data transfer.
Example
<?php
// PHP Variable
$phpValue = "Welcome to webstuffsolution.com"
?>
Read also: How to download file using JQuery?
jQuery Code
<script>
var jsValue = '<?php echo $phpValue; ?>';
console.log(jsValue);
$(document).ready(function() {
$("#outputValue").text(jsValue);
});
</script>
<div id="outputValue"></div>
I hope this tutorial help you.