-2

hello guys can u help me i want to echo my button but seems like my JS onclick function can't work

 echo "<th><button id="btnPrint" type="button" class="btn btn-primary " onclick="pindah('<?php echo $row->id ; ?>')">Detail Laptop &nbsp;<span class="fa fa-info"></span></button></th>"; 
Noel
  • 1
  • 3
  • 2
    Do you know how to escape quotation marks ? You may refer to this [link](https://stackoverflow.com/questions/7999148/escaping-quotation-marks-in-php) – Ken Lee Jul 24 '21 at 17:42

3 Answers3

1

(As stated by others already) You need to use the correct quotes. And concatenate your value instead of using <?php echo... when you already use echo to output:

echo '<th><button id="btnPrint" type="button" class="btn btn-primary" onclick="pindah(' . $row->id . ')">Detail Laptop &nbsp;<span class="fa fa-info"></span></button></th>';
brombeer
  • 8,716
  • 5
  • 21
  • 27
0

You need to escape the double quotes as the php statement is not completely error-free. Also please have a look at the below link for more details.

Link: echo and print in PHP

 echo "<th><button id=\"btnPrint\" type=\"button\" class=\"btn btn-primary \" onclick=\"pindah('<?php echo $row->id ; ?>')\">Detail Laptop &nbsp;<span class=\"fa fa-info\"></span></button></th>";
Ariel
  • 2,471
  • 1
  • 26
  • 43
-3

It will not work. JS is a client side scripting language. we cannot execute php code in javascript. please change your code in such a way it will do a post back on click or will make AJAX request on click.

Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
Rahul Cp
  • 56
  • 4
  • 2
    OPs code will work though. PHP outputs given string and "only" sets a parameter for a JS function – brombeer Jul 24 '21 at 17:49