dqlk31541 2018-01-19 16:14
浏览 55
已采纳

将PHP数组发送到在echoed string中调用的JS函数

I have a multidimensional array and want to send it as a parameter to JS function. Google told me that json_encode() is the way to go, but I get an unterminated string literal error when I try to call the function.

Here's my code:

$wynik_artefakty = $polaczenie->query("SELECT * FROM artefakty"); 
$tablica_artefakty = mysqli_fetch_all($wynik_artefakty,MYSQLI_ASSOC);
$tablica_artefakty = json_encode($tablica_artefakty);

 echo "<div class='wpis' 
       onclick=\"dodajDiv('$tablica_artefakty')\">" 
      .$tablica_jednostek[$i]['nazwa']." ".$tablica_jednostek[$i]['koszt']."</div>";

And the example echo of the array after json_encode():

[{"Id":"2","nazwa":"Sceptre of Atrocities","personal":"The 
Gorgon","dla_maga":"0","koszt":"15"},{"Id":"3","nazwa":"Artefakt dla 
maga","personal":"","dla_maga":"1","koszt":"10"}] 
  • 写回答

1条回答 默认 最新

  • douzhao7445 2018-01-19 16:22
    关注

    It's because the string you're injecting from $tablica_artefakty contains quotes. This causes your HTML be improperly rendered - attribute is prematurely closed by the quote from your string. You should escape the quotes in your $tablica_artefakty so that they don't close the HTLM attributes in wrong places.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号