Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php generated html onclick variable not working

so I have this php code

$listing .= "<button onclick='updateValue(".$id.", ".$key.")'>change value</button>";

$id is an integer and works just fine, key is a reference from a for loop and the value translate well in the html code however the key value table is recognised as a variable instead of a string.

HTML: onclick="updateValue(3, table)"

I have tried evey trick I know (creating new variable with quotes, adding single quotes in the onclick and (string)$key ) to convert it as a string but nothing is working so far. any idea please ? is it even possible ?

like image 417
jotyhista Avatar asked Jan 23 '26 10:01

jotyhista


1 Answers

You need to add quotes to tell javascript that it's a string. Since you're inside an echo you'll need to escape the quotes:

$listing .= "<button onclick='updateValue(".$id.", \"".$key."\")'>change value</button>";
like image 120
rickdenhaan Avatar answered Jan 26 '26 00:01

rickdenhaan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!