簡體   English   中英

如何從放置在 php 腳本中的 html 按鈕調用 Java Script 函數?

[英]How to call a Java Script function from a html button placed inside a php script?

我有一個代碼,其中一個 html 按鈕應該調用一個函數,該函數更改元素的樣式作為顯示它的一種方式,因為它默認情況下是隱藏的。 該按鈕位於需要條件才能顯示按鈕的 php 代碼中。 單擊該按鈕時該按鈕不起作用。 我已經在 php 代碼之外嘗試過它,它正確地調用了它的功能,所以它必須與 php 代碼有關...

<?php 
            if($_SESSION['privilege'] == "Standard User"){
                echo "<a href='index.php'><button class='userbtn'>Index</button></a>
                <a href='#'><button class='userbtn'>Check Orders</button></a>
                <a href='#'><button class='userbtn'>Add Order</button></a>";
                exit;
            }
            else if($_SESSION['privilege'] == 'Administrator'){
                echo "<a href='index.php'><button class='userbtn'>Index</button></a>
                <a href='#'><button class='userbtn'>Check Orders</button></a>
                <a href='#'><button class='userbtn'>Add Order</button></a>
                <button class='userbtn' onclick='callReg();'>Add User</button>";
                exit;
            }
            else{
                echo "<a href='index.php'><button class='userbtn'>Index</button></a>";
                exit;
            }
            ?>

我希望它可以工作,因為它在 php 代碼之外工作,但是當它放在 php 內部時它根本不起作用

您需要從 PHP 代碼中刪除exit語句。 出口的作用是停止腳本的執行。 如果文件包含在另一個中,則停止執行整個腳本,而不僅僅是包含的文件。

exit

一個例子:

文件1.php:

<html>...
Some code here...
<?php 
    include 'file2.php';
?>
Some other HTML code...
</html>

文件2.php:

<?php 
exit;
?>

然后文件File1.php的執行輸出將在包含的地方結束。 部分Some other HTML code...</html>永遠不會輸出到瀏覽器,因為腳本中途停止。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM