Dennis Ong
Dennis Ong

Reputation: 51

Passing PHP Variable echoing HTML

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>

Upvotes: 0

Views: 33

Answers (1)

Barmar
Barmar

Reputation: 781038

If $message_key is a string, you need to put quotes around it.

<button onclick='clear_alarm(\"" . $message_key . "\")' class='btn btn-sm white pull-right'> Clear </span> </button> </div>

Upvotes: 2

Related Questions