Home > Software design >  Sending php array to a javascript function with button onclick in html
Sending php array to a javascript function with button onclick in html

Time:12-23

Maybe it was a little confusing.

I have a datatable. In this datatable, there is an edit button at the end. Clicking on the edit button opens a modal. I want to pass the data in this datatable to the modal. I can send it as a single variable, but when I want to convert the data to an array and throw it away, I get an error.

My table:

<td><input type="text" value="<?= $datt['a']; ?>" id ="a" /> </td>
<td><input type="text" value="<?= $datt['b']; ?>" id ="b" /> </td>
<td><input type="text" value="<?= $datt['c']; ?>" id ="c" /> </td>  
<? $arry = array(
"a" => $datt['a'],
"b" => $datt['b'],
"c" => $datt['c']);  ?>
<td><button id="updd"  data-toggle="modal" data-target="#updd" onclick="show_fun(<?= $arr ?>)">Edit</button></td>

JS function:

function show_fun(datt){
$(document).on("click", ".updd", function (e) {
 var obj = datt;

 console.log(datt);

});

Result:

Array() { [native code] }

I also sent the Array with json_encode.

I tried

onclick="show_fun(<?= $arr ?>)"

instead of

onclick="show_fun(<?= json_encode($arr) ?>)"

Result:

undefined

How can I send data to modal?

CodePudding user response:

You don't need inner click listener. In HTML:

<button ... onclick="show_fun(<?= json_encode($arr); ?>);" />

In JS:

function show_fun(dat) {
 console.log(dat);
}
  • Related