How to access PHP variables in JQuery?
Hi Friends,
This article will give you an example of how to access php variable in jquery. I would like to share with you how to use php variable in jquery. This tutorial will give you a simple example of how to get php variable in jquery. I’m going to show you about how to print php variable in jquery.
If you are working on php and you need to print your php variable inside the jquery then you can simple get value. you can use "" for get value of php variable. in following example you can see :
Example Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>How to access PHP variables in JQuery?</title>
</head>
<body>
<?php
$simple = 'demo text string';
$myArray = array("One", "Two", "Three", "Four");
?>
<script type="text/javascript">
var simple ='<?php echo $simple; ?>';
console.log(simple);
var myArray = <?php echo json_encode($myArray); ?>;
console.log(myArray);
</script>
</body>
</html>
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Profile Image Upload Tutorial with Example
- Laravel TCPDF: Generate HTML to PDF File Example
- Laravel Carbon Count Days Between Two Dates Example
- Laravel Where Clause with Function Query Example
- How to Remove Numbers from String in PHP?
- How to Convert Camel Case to Snake Case in PHP?
- PHP Array Remove Keys and Keep Values Example
- PHP - Getting Started PHPUnit Test Example
- How to Send Mail using Zoho SMTP Server in Laravel?
- PHP Capture Screenshot of Website from URL Example
- How to Integrate Google Recaptcha with PHP Form?
- How to Remove Duplicate Values from Array in PHP?
- PHP Check Word Exist in String or Not Example
- How to Remove Null Values from Array in PHP?