How to assign Php variable value to Javascript variable?
Essentially:
<?php
//somewhere set a value
$var = "a value";
?>
<script>
// then echo it into the js/html stream
// and assign to a js variable
spge = '<?php echo $var ;?>';
// then
alert(spge);
</script>
How to assign php variable value into javascript variable
echo
it. But Remember to follow the sequence of definition -
<?php
$name= "Rama";
?>
<script type="text/javascript">
var jvalue = 'Hi, <?php echo $name; ?>';
</script>
<?php
$abc = "<script>document.write(jvalue)</script>";
echo $abc;
?>
how to assign php variable value to a javascript variable
What you are doing looks right, but special characters in your actual data may be causing problems, and will be insecure. You could make an array of data, and then use json_encode to escape it - e.g.
<?php
$jsColumns = array(
array('data'=>'Country'),
array('data'=>'Customer Name'),
array('data'=>'Order Number'),
array('data'=>'Address')
);
?>
then
$('#users').DataTable({
"columns": <?php echo json_encode($jsColumns); ?>,
"processing": true,
"serverSide": true,
"ajax": {
url: 'demo2.php',
type: 'POST'
}
});
How to assign php variable in JavaScript value assign?
You simply cannot do that, you need to understand the difference between client/server side programming, you cannot assign Javascript value to PHP variable, yea but you can assign PHP value to your javascript
How to assign php variable to javascript variable
you can assign a php variable to a javascript variable using the following method.
var address =<?php echo json_encode($zip) ?>;
on the other hand if you want to assign a php array to a javascript variable than:
var overallcontent = <?php echo json_encode($type); ?>;
where $type
is a php array.
How to assign javascript value to php variable without using cookie
try using this demo code
<script>
if (data) {
var a =data;
<?php $abc = "<script>document.write(a)</script>"?>
}
if (!data) {
console.log('no data');
}
</script>
<?php
echo $abc;
?>
Related Topics
How to Iterate Over Non-English File Names in PHP
PHP Fatal Error: Call to Undefined Function MySQLi_Stmt_Get_Result()
Set Session Variable Using JavaScript in PHP
Custom Order Status Background Button Color in Woocommerce 3.3 Admin Order List
How to Send Email from Localhost Using PHP on Linux
Swift Httppost Data Not Inserting to MySQL Database
How to Bind Multiple Parameters to MySQLi Query
How to Get Int Instead String from Form
Get Numbers from String with PHP
Differencebetween the PHP Open Tags "<=" and "<Php"/"<"
Http Options Request on Azure Websites Fails Due to Cors
Strange Behaviour with Numbers That Have a Leading Zero
Sort Multi-Dimensional Array by Specific Key
Best Methods to Clean Up a Hacked Site with No Clean Version Available
Php's JSON_Encode Does Not Escape All JSON Control Characters