从PHP调用函数(Javascript)

时间:2022-09-25 23:59:16

I have a javascript popup dialog box but is unable to utilize it within my php code. I tried everything I could think of but nothing works below is an example code snippet:

我有一个javascript弹出对话框但无法在我的PHP代码中使用它。我尝试了我能想到的一切,但下面没有任何工作是一个示例代码片段:

<?php
    $password = "sysadmin";

    if( isset( $_POST[ 'signin' ] ) )
    {
        if( $password == 'sysadmin' )
        {
            echo "<script type = 'text/javascript'>msgAlert( 'Congrats', 'Access Granted' );</script>";
        }
    }
?>

<html>
    <head>
        <title>Test for Javascript Function Call</title>
    </head>

    <body>
        <form action = "" method = "POST">
            <button name "signin" type = "submit">Sign In</button>
        </form>

        <script type = "text/javascript">
            function msgAlert( title, text )
            {
                alert( title + " : " + text );
            }
        </script>
    </body>
</html>

the code above is just a sample test, if I can get the code above to work maybe I can to call a boostrap javascript popup dialogbox cause it is really boring to use the default window.alert() dialog box. So what is wrong with the code? why is it not calling the necessary function? what should I do to be able to get the code running smoothly.

上面的代码只是一个示例测试,如果我可以让上面的代码工作,也许我可以调用一个boostrap javascript弹出对话框,因为使用默认的window.alert()对话框真的很无聊。那么代码有什么问题呢?为什么不调用必要的功能?我该怎么做才能让代码顺利运行。

on a side note, please no need to downpoint my question. This question may be redundant/stupid/annoying/unimportant and whatever to you, but I am new to web development so it is pretty much important to me...thanks for the help

在旁注,请不要忽视我的问题。这个问题可能是多余的/愚蠢的/烦人的/不重要的,无论你怎么样,但我是网络开发的新手,所以这对我来说非常重要...感谢你的帮助

2 个解决方案

#1


1  

Theres another mistake in your js code: You try to call the function before the dom is loaded. That may causes the problem. Do sth like this in your js:

您的js代码中的另一个错误:您尝试在加载dom之前调用该函数。这可能会导致问题。在你的js中这样做:

window.onload=function(){msgAlert("test");};

#2


-1  

You need to include the Javascript function inside php tags. Otherwise the Javascript function call will go out of scope. here's an example. N:B the code is not tested.

你需要在php标签中包含Javascript函数。否则Javascript函数调用将超出范围。这是一个例子。 N:B代码未经过测试。

<?php
    $password = "sysadmin";
    if( isset( $_POST[ 'signin' ] ) )
        {
            if( $password == 'sysadmin' )
                {

                    echo "<script type = 'text/javascript'>
function msgAlert( title, text )
            {
                alert( title + ' : ' + text );
            }msgAlert( 'Congrats', 'Access Granted' );</script>";
                }
        }
?>
   <!-- remaining html -->

#1


1  

Theres another mistake in your js code: You try to call the function before the dom is loaded. That may causes the problem. Do sth like this in your js:

您的js代码中的另一个错误:您尝试在加载dom之前调用该函数。这可能会导致问题。在你的js中这样做:

window.onload=function(){msgAlert("test");};

#2


-1  

You need to include the Javascript function inside php tags. Otherwise the Javascript function call will go out of scope. here's an example. N:B the code is not tested.

你需要在php标签中包含Javascript函数。否则Javascript函数调用将超出范围。这是一个例子。 N:B代码未经过测试。

<?php
    $password = "sysadmin";
    if( isset( $_POST[ 'signin' ] ) )
        {
            if( $password == 'sysadmin' )
                {

                    echo "<script type = 'text/javascript'>
function msgAlert( title, text )
            {
                alert( title + ' : ' + text );
            }msgAlert( 'Congrats', 'Access Granted' );</script>";
                }
        }
?>
   <!-- remaining html -->