Tutorial Details

How to access PHP variables in JQuery
25 Nov

How to access PHP variables in JQuery

Hi Guys,

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 variables in jQuery. This tutorial will give you a simple example of how to get a php variable in jQuery. I’m going to show you how to print php variables in jQuery.

If you are working on PHP and you need to print your php variable inside the jQuery then you can simply get value. you can use "" to get the value of the PHP variable. In the 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...

0 Comments

Leave a Comment