I have a index.php page that has the following code in it:
<table id="example" class="display" width="100%"></table>
<?php
echo "<div id='example'></div>";
?>
<script type="text/JavaScript">
var mtsize10 = new XMLHttpRequest();
mtsize10.open('POST', 'getvals.php?checks__get it now', false); // `false` makes the request synchronous
mtsize10.send(null);
if (mtsize10.status === 200) {// That's HTTP for 'ok'
var mytsize1 = mtsize10.responseText;
var sfullslist = mytsize1;
var zfullslist = sfullslist.split("
");
var fullslist = zfullslist[0].split(",");
}
</script>
<script src="myjsscript.js"></script>
In the above index.php script, I'm calling a javascript myjsscript.js
which has the following in it:
var dataSet = [
fullslist
];
$(document).ready(function() {
$('#example').DataTable( {
data: dataSet,
columns: [
{ title: "v1" },
{ title: "v2" },
{ title: "v3" },
{ title: "v4" },
{ title: "v5" },
{ title: "v6" }
]
} );
} );
Note that I specified a variable called fullslist
which has not be defined yet. It will be defined when the index.php
page is loaded.
And when that happens, the getvals.php
script specified in the index.php
page spits out multiple lines that look like this:
mary,accountant,35,140lb,brunette,married
helen,clerk,21,120lb,blonde,married
harry,student,19,200lb,blonde,single
In the index.php
file, I'm only grabbing the first line because I dont know how to do this dynamically. By "dynamic", I'm asking to know how I can automatically define variables so that i can create as many fullslist
as needed. I want to display all the lines that the getvals.php
script produces. Right now, I'm only displaying the first line because its all I know how to do. I hardcoded that in. I'd like to avoid that and display everything the getvals.php
script outputs.
I have no idea if I'm even asking the question correctly. I'm new to web development so please take it easy on me. I've probably broken series of rules by how this is set up. But I'm learning.
question from:
https://stackoverflow.com/questions/66057682/create-dynamic-javascript-variables-in-html-php-through-sourced-js-file