简体   繁体   English

如何将数组从 php 输入 javascript 函数并使用 HTML 显示输出?

[英]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.

相关问题 使用Java脚本显示输入的特定输出(来自HTML) - To display specific output for an input (from HTML) using Javascript 如何从Javascript / Typescript中的数组对象计算运行总计并使用HTML在每个实例中显示输出? - How to calculate running total from an array object in Javascript/Typescript and display output at every instance using HTML? 如何使用javascript显示来自HTML文本框的输入 - How to display an input from a HTML textbox using javascript 如何使用 html 输入字段从 php 代码显示 javascript 数组中的列表? - How do i show a list in a javascript array from a php code using a html input field? 如何使用 JavaScript 显示 html 输入:.html(&#39; &lt; input type=&quot;text&quot; /&gt;&#39;) - How to display a html input using JavaScript : .html(' < input type="text" />') 使用 javascript 在 html 输入标签中显示 php 会话的值 - display the value of a php session in a html input tag using javascript Javascript HTML输入数组PHP - Javascript HTML input array PHP 如何使用 javascript 在页面中输入并在 html 中显示输入? - How to take input in a page using javascript and display the input in the html? PHP数组输出到javascript函数 - PHP array output to a javascript function HTML/Javascript - 如何将输入字段中的文本显示为 HTML 代码? - HTML/Javascript - How to display text from input field as HTML code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM