I am trying to pass a value from a php variable to a javascript variable. So instead of defining manually each time {"data": "Country"},{"data": "Customer Name"}... as you see below
<script type="text/javascript">
$(document).ready(function () {
$('#users').DataTable({
"columns": [
{"data": "Country"},
{"data": "Customer Name"},
{"data": "Order Number"},
{"data": "Address"}
],
"processing": true,
"serverSide": true,
"ajax": {
url: 'demo2.php',
type: 'POST'
}
});
});
</script>
Let's say I have a php variabe defined on the top of my html as below
<?php
$jsColumns = '{"data": "Country"},{"data": "Customer Name"},{"data": "Order Number"},{"data": "Address"}';
?>
I want to insert this php variable inside my script
$('#users').DataTable({
"columns": [
<?php echo $jsColumns;?>
],
"processing": true,
"serverSide": true,
"ajax": {
url: 'demo2.php',
type: 'POST'
}
});
It's not working inside the script but when i use echo $jsColumns;
on the top in the php part i can see that the variable contains value.
Any idea please what i am missing in my code ? Thank you very much.