Reputation: 44
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>
Upvotes: 0
Views: 116
Reputation: 46602
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>
Upvotes: 5