-1

I am trying to run a javascript function inside my PHP code but the javascript function is not calling, or not working, but when I try to run it without the PHP it works fine:

<div class="chatbox">
 <div class="chatbox_head">chat</div>
 <div class="chatbox_body">
 <?php 
 $n = 'Ozoibekwe';
 $m = 'joy';
 echo '
 <div class="sidebar-name">
 <!-- Pass username and display name to register popup -->
 <a href="javascript:register_popup('.$n.', '.$m.');">
 <img width="30" height="30" src="user_pix/david.jpg" />
 <span>Ozoibekwe joy</span>
 </a>
 </div><br> ';
 ?>
 </div> 
</div>
khelwood
59.7k14 gold badges91 silver badges116 bronze badges
asked Aug 30, 2017 at 1:01
2
  • You probably want quotes around $n and $m. Of course, your code is a bad practice anyways. Commented Aug 30, 2017 at 1:15
  • Please don't vandalise your question @ELOIKEDAVID Commented Sep 27, 2020 at 22:45

1 Answer 1

5

You need to quote the strings in your javascript function:

javascript:register_popup(\''.$n.'\', \''.$m.'\');:

<div class="chatbox">
 <div class="chatbox_head">chat</div>
 <div class="chatbox_body">
 <?php 
 $n = 'Ozoibekwe';
 $m = 'joy';
 echo '
 <div class="sidebar-name">
 <!-- Pass username and display name to register popup -->
 <a href="javascript:void(0);" onClick="register_popup(\''.$n.'\', \''.$m.'\');">
 <img width="30" height="30" src="user_pix/david.jpg" />
 <span>Ozoibekwe joy</span>
 </a>
 </div><br> ';
 ?> 
 </div> 
</div>

Alternatively, you could do it like this, which is much cleaner:

<?php 
$n = 'Ozoibekwe';
$m = 'joy';
?>
<div class="chatbox">
 <div class="chatbox_head">chat</div>
 <div class="chatbox_body">
 <div class="sidebar-name">
 <!-- Pass username and display name to register popup -->
 <a href="javascript:void(0);" onClick="register_popup('<?= $n ?>', '<?= $m ?>');">
 <img width="30" height="30" src="user_pix/david.jpg" />
 <span>Ozoibekwe joy</span>
 </a>
 </div>
 <br>
 </div> 
</div>
answered Aug 30, 2017 at 1:04
Sign up to request clarification or add additional context in comments.

2 Comments

i tried that but its not working, i cannot call the javascript function
@ELOIKEDAVID See edit, changed to onClick event, try that.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.