简体   繁体   English

如何将PHP数组作为参数传递给JavaScript函数

[英]How can I pass a PHP array as an argument to a JavaScript function

It's just an example, but with this code, it works. 这只是一个示例,但是使用此代码,它可以工作。 But if I change 5 to "5" and 10 to "10" I got an Unexpectid ending syntax error for the echo line 但是,如果我将5更改为“ 5” ,将10更改为“ 10” ,则回显行出现意外的结尾语法错误

PHP 的PHP

$array = array();
$array[0] = 10;  
$array[1] = 5; 
$json = json_encode($array);

echo "<td style=\"background: red;\"><button onclick=\"modifyModalContent('$day_date_column','$i',$json) ... (really long line)

JS JS

function modifyModalContent(date, id, array) {
  var header = document.querySelector(".modal-header");
  var body = document.querySelector(".modal-body");

  var table =
  `
  <table class="table table-bordered table-hover">
    <tr>    
      <td>${array[0]}<td>
      <td>${array[1]}<td>
    </tr>
  </table>
  `;

  body.innerHTML = table;

  header.innerHTML = `<h1> Date: ${date}</h1></br><h1>ID: ${autoid}</h1>`;
}

The two echo line after run: 运行后的两条回声线:

That works: 这样可行:

<tr style="height: 137px;"><td style="background: red;"><button onclick="modifyModalContent('2019-01-21','1',[10,5])" ... (long line) 

And if I use it with a string array got error on this: 如果我将它与字符串数组一起使用,则会出现以下错误:

<tr style="height: 137px;"><td style="background: red;"><button onclick="modifyModalContent(2019-01-21,1,["10","5"])" ... (long line) 

How could I do the same with an array like this: 我如何用这样的数组做同样的事情:

$array = array();
$array[0] = "10";  
$array[1] = "5"; 

I guess the problem is because of the " char, but How could I fix it? 我猜问题出在“ char”,但是我该如何解决?

我建议使用PHP函数htmlspecialchars -不仅要解决引号问题,还应解决在用作HTML属性值之前应转换为HTML实体的任何其他字符(&,<,>}。

$json = htmlspecialchars(json_encode($array));

Replace ["10","5"] with ['10','5'] ["10","5"]替换为['10','5']

More information about simple and double quotes: When to use double or single quotes in JavaScript? 有关单引号和双引号的更多信息: 何时在JavaScript中使用双引号或单引号?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从PHP端传递JavaScript函数中的数组字符串作为参数? - how to pass array string in JavaScript function from PHP end as a argument? 如何将 php 变量作为参数传递给 javascript function - How to pass a php variable as an argument to a javascript function 如何将数组作为参数传递给javascript中的函数? - How to pass an array as argument to a function in javascript? 如何传递数组作为javaScript中的参数? - How to pass array to function as argument in javaScript? 单击按钮后如何将Javascript数组传递给PHP数组? - How can I pass Javascript array to a PHP array on button click? JavaScript:如何将字符串参数传递给函数以用于按该字符串参数的值对对象数组进行排序? - JavaScript: How do I pass a string argument to a function to use to sort an array of objects by the value of that string argument? 我可以使用Javascript数组对象作为函数中的参数吗? 怎么样? - Can I Use A Javascript Array Object As An Argument In A Function? How? 如何使用json_encode()将php关联数组作为参数传递给Javascript函数 - How to pass an php associative array as argument to Javascript function with json_encode() 如何使用处理程序将参数传递给 function? - How can I pass an argument into a function with a handler? 如何将多个变量传递给HTML / PHP中的JavaScript函数? - How can I pass multiple variables to a JavaScript function in HTML / PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM