页面加载时会自动调用PHP函数,而不是通过onclick事件调用

时间:2012-10-27 23:02:50

标签: php javascript session web-applications post

我正在尝试将网页用户重定向到两个不同页面中的一个,具体取决于他们点击的两个按钮中的哪一个。我的方法是将每个onclick事件链接到一个javascript函数,然后调用一个php函数,该函数使用header()函数重定向到适当的页面。不幸的是,每次页面加载和重定向时,都会自动调用两个php函数中的后者,即insertIntoTable(),而不会让用户甚至用按钮查看页面。我想知道这是否是PHP扩展的工件?我真的不明白,因为看起来包装javascript函数不应该被调用,直到onclick事件监听器这样做,并且直到javascript函数链接才应该调用php函数onclick这样做。这是代码......

<?php
    session_start();
?>
<html>
    <head>
        <script>
            function createATable()
            {
                <?php createATable(); ?>
            }

            function insertIntoTable()
            {
                <?php insertIntoTable(); ?>
            }
        </script>
    </head>
    <body>
        <?php
            // Define our redirection functions for button click events
            function createATable()
            {
                header("Location: http://codd.edu/Project2/createtable.php?<?php echo htmlspecialchars(SID); ?>");*/
            }
            function insertIntoTable()
            {
                header("Location: http://codd.edu/Project2/insertintotable.php?<?php echo htmlspecialchars(SID); ?>");*/
            }

            // Set session variables so that we don't need to worry about whether
            // we're coming from sign in or registration - if $Session['user_name'] 
            // isn't set, we know were coming from the sign in
            if (!$_SESSION['user_name'])
            {
                $_SESSION['user_name'] = $_POST['nametextbox'];
                $_SESSION['user_psswd'] = $_POST['psswdtextbox'];
                echo "<br />setting session variables";
            }
            echo "<br />Not setting session variables";

            // If we aren't setting the session variables, then we are coming from the 
            // registration page, and therefore know that this isn't an admin account
            $_SESSION['is_admin'] = "false";

            // Connect to database
            $conn = mysql_connect("localhost", "601", "23");
                or die('Could not connect: ' . mysql_error());

            // Open database
            mysql_select_db("601", $conn) 
                or die('Could not find database: ' . mysql_error());

            // Get USER tuples, if any
            $user = mysql_query("SELECT * FROM USERS WHERE name='$_SESSION[user_name]' AND password='$_SESSION[user_psswd]'");

            // If there are no entries for this SELECT command, we cannot 
            // allow the user to log in 
            $num_user = mysql_num_rows($user);
            if ($num_user < 1) 
            {
                $_SESSION['is_user'] = "false";
                header("Location: http://codd.edu/Project2/login.php?<?php echo htmlspecialchars(SID); ?>");
                exit;
            } 
            else 
            {
                $_SESSION['user_id'] = SID;
                $_SESSION['is_user'] = "true";
                echo "Welcome ", $_SESSION['user_name'], "!";
                echo "<br />User ID: " . $_SESSION['user_id'];
                echo "<br /> User Password: " . $_SESSION['user_psswd'];
                echo "Is valid user? " . $_SESSION['is_user'];
                session_destroy();

                echo "<button name='createtable' onclick='createATable()'>Create a Table</button>";
                echo "<br /><button name='insert' onclick='insertIntoTable()'>Insert into Table</button>";
            }
        ?>
    </body>
</html>

2 个答案:

答案 0 :(得分:3)

我认为没有理由在页面加载时不应该调用createATable()。 PHP在javascript之前和不同的环境中进行评估。此外,PHP允许您在调用之后定义/声明一个函数,这正是您正在做的事情。

假装是PHP解释器:它不知道javascript是什么。您甚至可以在<?php createATable(); ?>之前和之后包裹yadayadayada ... qweryasdasd行,它将会运行。

请阅读此内容并查看是否有帮助:http://www.developer.com/tech/article.php/923111/Client-side-Versus-Server-side-Coding---Part-1.htm

PS:这与您的问题无关,但请看一下:http://en.wikipedia.org/wiki/SQL_injection

答案 1 :(得分:1)

Php在服务器上运行,html输出发送到客户端。

不使用php来重定向,而是使用javascript或简单的href。