0

2つのボタンのどちらをクリックするかに応じて、Webページのユーザーを2つの異なるページのいずれかにリダイレクトしようとしています。私のアプローチは、各onclickイベントをjavascript関数にリンクすることでした。この関数は、header()関数を使用して適切なページにリダイレクトするphp関数を呼び出します。残念ながら、後者の2つのphp関数、つまりinsertIntoTable()は、ページが読み込まれてリダイレクトされるたびに自動的に呼び出され、ユーザーがボタンでページを見る機会さえ与えられません。これはPHPが拡張される方法のアーティファクトであるかどうか疑問に思いますか?onclickイベントリスナーが呼び出すまでラッピングjavascript関数を呼び出すべきではなく、リンクされたjavascript関数を呼び出すまでphp関数を呼び出すべきではないように思われるため、私は本当に理解していません。オンクリックするにはそうします。これがコードです...

<?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>
4

2 に答える 2

2

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

于 2012-10-27T23:06:48.607 に答える
0

PHP はサーバー上で実行され、htnl 出力はクライアントに送られます。

php を使用してリダイレクトする代わりに、javascript または単純な a href を使用してください。

于 2012-10-27T23:17:41.513 に答える