dpzlz08480 2012-10-27 23:02
浏览 74
已采纳

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

I'm trying to redirect users of a web page to one of two different pages, depending on which of two buttons they click. My approach was to link each onclick event to a javascript function, which would in turn call a php function which uses the header() function to redirect to the appropriate page. Unfortunately, the latter of the two php functions, namely insertIntoTable(), is being called automatically each time the page loads and redirecting, without giving the user a chance to even look at the page with the buttons. I'm wondering if this is an artifact of way PHP is expanded? I don't really understand, becauseit seems like the wrapping javascript function shouldn't be called until the onclick event listener does so, and the php functions shouldn't be called until on the the javascript functions linked to onclick do so. Here is the code...

<?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条回答 默认 最新

  • dshp9580656 2012-10-27 23:06
    关注

    I see no reason why createATable() shouldn't be called anytime the page is loaded. PHP is evaluated before javascript and on a different environment. Moreover, PHP allows you do define/declare a function after you call it, that is exactly what you're doing.

    Pretend to be the PHP interpreter: it has no idea of what javascript is. You can even wrap the <?php createATable(); ?> line with yadayadayada ... qweryasdasd before and after it, and it will run.

    Please, read this and see if it helps: http://www.developer.com/tech/article.php/923111/Client-side-Versus-Server-side-Coding---Part-1.htm

    PS.: this is not related to your question, but please take a look at this: http://en.wikipedia.org/wiki/SQL_injection

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看