我的javacript函数没有在我的PHP代码中运行

时间:2017-08-30 01:01:51

标签: javascript php html5

我正在尝试在我的PHP代码中运行javascript函数,但是javascript函数没有调用,或者没有工作,但是当我尝试在没有PHP的情况下运行它时,它工作正常:

<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>

1 个答案:

答案 0 :(得分:5)

您需要在javascript函数中引用字符串:

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>

或者,你可以这样做,这更清洁:

<?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>