How to Assign PHP Variable Value to JavaScript Variable

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



Leave a reply



Submit