简体   繁体   中英

How to send an array as param from smarty template in javascript function

Problem description:

I have an Array($all_article_data) in a .tpl File with 200 items. Now I want to send this array as Param in a javascript function.

<span onclick="on('{$all_article_data}')">Example</span>

JAVASCRIPTFUNCTION

function on(data){
     alert(data);
}

And I recive in text "array" :( To see if there is something in this array i used:

function on(data) {
   var i;
   for(i=0 ; i<=data.length; i++){
       alert(data[i]);
   }
}

This shows in differents alerts array I need to manipulate this array in javascript and create a table with the values.

Thanks for Tipps

Best regards

You should use php's json_encode on your array:

$encoded= json_encode($all_article_data);

<span onclick="on('{$encoded}')">Example</span>

Then in your javascript, parse it back:

let jsonData = JSON.parse(data);

You can then use jsonData as you intend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM