简体   繁体   English

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

[英]PHP function is automatically called when page loads, instead of being called through onclick event

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. 我的方法是将每个onclick事件链接到javascript函数,该函数随后将调用php函数,该函数使用header()函数重定向到相应的页面。 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. 不幸的是,每次页面加载和重定向时,都会自动调用两个php函数中的后者,即insertIntoTable(),而没有给用户提供甚至使用按钮查看页面的机会。 I'm wondering if this is an artifact of way PHP is expanded? 我想知道这是否是PHP扩展方式的产物? 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. 我不太了解,因为似乎只有在onclick事件监听器调用包装的javascript函数之前,才应该调用包装的javascript函数,而在链接到onclick的javascript函数上才应该调用php函数。 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>

I see no reason why createATable() shouldn't be called anytime the page is loaded. 我认为没有理由为什么在页面加载时不应该调用createATable() PHP is evaluated before javascript and on a different environment. PHP在使用JavaScript之前和在其他环境中进行评估。 Moreover, PHP allows you do define/declare a function after you call it, that is exactly what you're doing. 而且,PHP允许您在调用函数后定义/声明一个函数,这正是您正在做的事情。

Pretend to be the PHP interpreter: it has no idea of what javascript is. 伪装成PHP解释器:它不知道什么是javascript。 You can even wrap the <?php createATable(); ?> 您甚至可以包装<?php createATable(); ?> <?php createATable(); ?> line with yadayadayada ... qweryasdasd before and after it, and it will run. <?php createATable(); ?>前后都有yadayadayada ... qweryasdasd行,它将运行。

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 请阅读此内容,看看是否有帮助: 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 PS .:这与您的问题无关,但是请看一下: http : //en.wikipedia.org/wiki/SQL_injection

Php runs on the server, the htnl output goes to the client. Php在服务器上运行,htnl输出发送到客户端。

Instead of trying to use php to redirect, use javascript or a simple a href. 与其尝试使用php进行重定向,不如使用javascript或简单的href。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的onclick事件是在页面加载时执行的,而不是在单击按钮时执行的,我不知道为什么 - My onclick event performs when the page loads instead of when the button is clicked, and i don't know why javascript中未调用javascript函数 - javascript function not being called in php PHP:第二个函数未被调用 - PHP : Second Function not being called PHP回调函数未被调用 - PHP callback function not being called netbeans没有逐步执行被调用的函数 - netbeans not stepping through the function being called 如何找到页面加载时调用的所有 php 文件的列表? - How can I find a list of all the php files that are called when a page loads? Drupal 7 Ajax无法正常工作(调用了成功函数,但正在打印drupal页面,而不是页面回调结果) - Drupal 7 ajax not working properly (success function called but the drupal page is being printed instead of the page callbacks results) onclick函数未调用 - onclick function not getting called 无法从HTML页面调用Javascript函数 - Javascript function not being called from html page 我正在尝试从onClick事件调用javascript函数,但据我所知该函数没有被调用。 - I'm trying to call a javascript function from the onClick event but as far as I know the function isn't being called.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM