How To Pass PHP Variables To JavaScript Example
Hi guys,
Today i will explained How To Pass PHP Variables To JavaScript. This example is so easy to use in php. This example to i am pass to the php variable in javascript and alert to the variable value.
So let's start to the example.
Example 1
<?php
$message = "Hello World!"; // here you get data from database
?>
<!DOCTYPE html>
<html>
<head>
<title>How To Pass PHP Variables To JavaScript Example - Niceshippest.com</title>
</head>
<body>
<h1>How To Pass PHP Variables To JavaScript Example</h1>
<script type="text/javascript">
var message = "<?php echo"$message"?>";
alert(message);
</script>
</body>
</html>
Example 2
<?php
$fruits = ['orange' => 'mango', 'yellow' => 'banana', 'red' => 'apple'];
$json_fruits = json_encode($fruits);
?>
<!DOCTYPE html>
<html>
<head>
<title>How To Pass PHP Variables To JavaScript Example - Niceshippest.com</title>
</head>
<body>
<h1>How To Pass PHP Variables To JavaScript Example</h1>
<script type="text/javascript">
var fruits = <?php echo($json_fruits)?>;
alert(fruits.yellow);
</script>
</body>
</html>
Now you can check your own.
- How to set Date in setUTCHours() Method in JavaScript?
- How to Date sets UTC milliseconds in Javascript?
- How to Use push(), unshift(),pop() and shift() With Array in Javascript?
- How to Check if a Variable is a Number in Javascript?
- How To Format a Number Using Fixed - Point Notation in Javascript?
- How to Insert an Element into an Array in javaScript?
- How to remove duplicate elements from JavaScript Array?
- How to calculate years, months and days between two dates in javascript?