简体   繁体   中英

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

I have a code where an html button is supposed to call a function that changes the style of an element as a way to display it because it is hidden by default. The button is located inside a php code that requires a condition in order to show the button. The button is not working when it is clicked. I have tried it outside of the php code and it calls it's function properly so it have to be something with the php code...

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

I expected it to work as it was working outside the php code but it simply doesn't work when it is placed inside the php

You need to remove the exit statements from your PHP code. What the exit does is it stops the execution of the script. If the file was included in another in stops the execution of the whole script, and not just the included file.

exit

An example:

File1.php:

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

File2.php:

<?php 
exit;
?>

Then the output of the execution of file File1.php would end right where the include is. The part Some other HTML code...</html> would never be outputted to the browser, because the script stopped halfway through.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM