This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
                                
                                    (20个答案)
                                
                        
                                5年前关闭。
            
                    
我试图将PHP变量传递给Javascript函数,但它始终显示为“未定义”。我在单独的页面上有Javascript,并将它包含在我从中调用函数的页面上。这是代码:

<?php $repname    = $_SESSION['REPNAME']; ?>
<script type="text/javascript" src="js/chat.js"></script>

<input type="button" name="btn_send_chat" id="btn_send_chat" value="Send" onclick="javascript:sendChatText('<?php echo $repname;?>');" />


这是js / chat.js:

function sendChatText(repname) {
    alert(repname);

        }

最佳答案

您正在设置$ repid但回显$ repname

10-07 12:38