03-Apr-2023
.
Admin
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.
I hope it can help you...
#PHP 8
#JavaScript
#PHP