you need to transfer a two-dimensional array from php to js

array formation

while($res2 = mysql_fetch_array($res)) { $count++; $arr[] = $res2; } 

Js function

 function ShowTable(ind) { //$("[name = "+ind+"]").css('display', 'block'); alert(ind['index']); $("tr[name = "+ind['index']+"]").after( '<tr name="$tId" onclick="ShowTable($tId)">'+ '<td><input type = "checkbox" name = "$tId"></input>'+ '<td></td>'+ '<td><?=$arr[$i]['index']?></td>'+ '<td>$tSource</td>'+ '<td>$tMessage</td>'+ '<td>$tDate</td>'+ '<td>s</td>'+ '<td><button type="button" class="btn btn-danger btn-xs active">Stop</button></td>'+ '</tr>'); } 

I'm trying to convey

 <? for($i = 0; $i < $count; $i++) { for($j = 0; $j < $count; $j++) { if($arr[$j]['index'] == $index1) $indexCount++; else $index1 = $arr[$j]['index']; } if($arr[$i]['index'] != $index) { ?> <tr name="<?=$arr[$i]['index']?>" onclick="ShowTable(<?=$arr[$i]?>)"> <td><input type = "checkbox" name = "<?=$arr[$i]['index']?>"></input> <td><?=$arr[$i]['index']?></td> <td><?=$arr[$i]['source']?></td> <td><?=$arr[$i]['message']?></td> <td><?=date('Ymd H:i:s', $arr[$i]['timeToSend'])?></td> <td><?=$indexCount?></td> <td><button type="button" class="btn btn-danger btn-xs active" onclick="StopSend(<?=$arr[$i]['index']?>)">Stop</button></td> </tr> <? } $index = $arr[$i]['index']; } ?> 

on exit I get the value ind['index'] undefined, what am I doing wrong?

  • Instead of <?=$arr[$i]['index']?> Pass <?=$arr[$i]?> . You are accessing a non-existent index in JS. You also have an error in the string '<td><?=$arr[$i]['index']?></td>' . With quotes trouble. Just in case, I remind you that php-code is not executed in JS. - VenZell
  • The fact of the matter is that I am passing onclick="ShowTable(<?=$arr[$i]?>)" But nothing comes of it ... and then with quotes I’ll figure it out, you first need to get an alert to get something sensible - Anton Burak
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky
  • one
    Never write like this, transfer data via ajax - Serge Esmanovich

1 answer 1

Transfer data like this (with PHP 5.3):

 onclick="StopSend(this.getAttribute('data-source'))" data-source='<?= json_encode($arr[$i], JSON_HEX_QUOT); ?>' 

And read this:

 function ShowTable(ind) { var data = JSON.parse(ind); alert(data['index']); // Ваш действия } 
  • I have already presented what will be in html`ke) - Vasily Barbashev
  • one
    @ Vasily Barbashev, better not worth it. Take care of your nerves. :) Man learns, all code is far from perfect - VenZell
  • I tried to do that before ... - Anton Burak
  • I tried it now, when I click on the table, nothing happens ... - Anton Burak
  • @ AntonBurak, look at the console, what kind of errors are there? - VenZell