Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

Create dynamic javascript variables in html/php through sourced .js file

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...