[英]How to input array from php into javascript function and display the output using HTML?
I am trying to take the values from the array in PHP, and pass them onto the JavaScript function.我正在尝试从 PHP 中的数组中获取值,并将它们传递给 JavaScript 函数。 The code below functions as intended.
下面的代码按预期运行。 The values are taken from a MySQL database and entered into their respective arrays.
这些值取自 MySQL 数据库并输入到它们各自的数组中。 [Disclaimer: New to coding]
[免责声明:编码新手]
echo "<div id='demo'></div>";
$sql = mysqli_query($conn, "SELECT DISTINCT Safepoint_name,Latitude,Longitude FROM safepoints");
while($row = mysqli_fetch_array($sql)) {
$latl[] = $row['Latitude'];
$long[] = $row['Longitude'];}
foreach($latl as $name) {
echo "$name";
}
foreach($long as $name) {
echo "$name";
}
The problem I am having is with code as shown below.我遇到的问题是代码如下所示。 It does not process and show the output as desired.
它不会根据需要处理和显示输出。 I want the output to be shown one after the other after subtracting the two arrays.
我希望在减去两个数组后一个接一个地显示输出。 I would prefer if the output could be designed via HTML.
如果输出可以通过 HTML 设计,我会更喜欢。 What am I supposed to do?
我应该做些什么? [Note: The function is just a trial function just for checking whether it works]
[注:该功能只是一个试用功能,只是为了检查它是否有效]
<p id="demo"></p>
<button onclick = "myFunction()">Submit</button>
<script type="text/JavaScript">
let text = "";
function myFunction() {
var latitute = <?php echo json_encode($latl); ?>;
var loni = <?php echo json_encode($long); ?>;
for (let i = 0; i < latitute.length; i++)
{
var x[i] = latitute[i]-loni[i];
text += x[i]+"<br>";
}
document.getElementById("demo").innerHTML = text;
}
</script>
I think your problem here is that you are defining variable x
in a cycle (first warning something is fishy) and you are assigning a value into it's array index at the same time (which is technically possible, but not recommendable, in PHP, but definitely not in JS where is causes syntax error).我认为你的问题是你在一个循环中定义变量
x
(第一个警告是可疑的)并且你同时为它的数组索引分配一个值(这在技术上是可能的,但在 PHP 中是不推荐的,但是绝对不会在 JS where is 导致语法错误)。
You can skip the variable completely and just generate the text.您可以完全跳过变量而只生成文本。
for (let i = 0; i < latitute.length; i++) {
text += (latitute[i]-loni[i]) +"<br>";
}
Or if you need to keep the result array for later, you should define it as an array before the cycle:或者,如果您需要保留结果数组以供以后使用,则应在循环之前将其定义为数组:
var x = [];
for (let i = 0; i < latitute.length; i++) {
x[i] = latitute[i]-loni[i];
text += x[i]+"<br>";
}
Update:更新:
<p id="demo"></p> <button onclick = "myFunction()">Submit</button> <script type="text/JavaScript"> console.log('Preparing JS...'); let text = ""; function myFunction() { console.log('Calculating demo...'); var latitute = [9,8,7]; var loni = [1,2,3]; for (let i = 0; i < latitute.length; i++) { text += (latitute[i] - loni[i]) + "<br>"; } document.getElementById("demo").innerHTML = text; alert('Demo OK'); } </script>
JS runs OK after submit. JS 提交后运行正常。
Right-Click your page and use "View page source file" to check what PHP generates into the JS - if it's really valid array as in the example above.右键单击您的页面并使用“查看页面源文件”来检查 PHP 生成到 JS 中的内容 - 如果它确实是上面示例中的有效数组。
Then open Debugger (press F12 in browser) and see Console if there are any Syntax or other errors.然后打开调试器(在浏览器中按 F12)并查看控制台是否有任何语法或其他错误。
If you are in doubt if JS is running, you can add console.log()
and/or alert()
into your code (see the example).如果您不确定 JS 是否正在运行,您可以将
console.log()
和/或alert()
添加到代码中(参见示例)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.