I have a .PHP file and wanted to echo HTML. I did echo it perfectly but now i want to echo a javascript onclick= print( $variable) . I keep getting error or invalid syntax.
the button on click won't work when i pass in a php variable but if i pass in manually it works.
Thank you.
Below are my code.
<div class="streamline b-l m-l">
<?php
$messages = $user_projects_json['projects'][$projectKey]['alarms'];
foreach($messages as $message_key => $messagesList){
echo "
<div class='sl-item b-success'>
<div class='sl-icon'>
<i class='fa fa-check'></i>
</div>
<div class='sl-item b-info'>
<div class='sl-content'>
<div class='sl-date text-muted'>". $messagesList['t'] ."
<span class='pull-right'>Name: " . strtoUpper($messagesList['n']) . " </span>
</div>
<div> " .$messagesList['m'].
"<span>
<button onclick='clear_alarm(" . $message_key . ")' class='btn btn-sm white pull-right'> Clear </span> </button> </div>
<div>
</div>
</div>";
}
?>
</div>
". If the value of$message_keyis a string, your source is going to look likeonclick='clear_alarm(mystringvalue)'. In which case javascript is going to treat that string as a variable reference, not a literal.