Welcome to findnerd, today we are going to discuss How to pass variables and data from PHP to JavaScript.
In this blog we demonstrate and describe passing PHP variables containing simple data types to JavaScript.
There are actually several approaches to do this.There are some conditions when pass variables
and data from PHP to JavaScript that data and variable always encode with json_encode otherwise error may be generate.
So the PHP json_encode function can be used to resolve these problems.
Yoa can take reference of bellow example:
There is a php code in tn which two varibale are avaiable
<?php
//here ctrate a variable name is $sum
$sum = 10 + 5;
//here ctrate a variable name is $Name
$Name = "Jorden";
?>
<script type="text/javascript">
//here write a javascript variable and pass php variable
var sumdata=<?php echo json_encode($sum); ?>; //write here the extra semicolon!
//here write a javascript variable and pass php variable
var Getname=<?php echo json_encode($sum); ?>;//write here the extra semicolon!
</script>
0 Comment(s)