绑定按钮单击()事件到PHP函数

时间:2013-12-23 10:10:21

标签: php html forms button

我是PHP的新手,刚刚开始学习这门语言的基础知识。

我创建了一个包含文本字段和密码字段的页面login.php。我想要做的是,在单击提交按钮时,我想调用我在同一页面上定义的PHP方法authenticateUser()。如果用户成功通过身份验证,则会将其定向到index.html页面。如果没有,那么他将保持在同一页面上,并弹出一条错误消息,提示用户名/密码不正确。所有这些逻辑都在authenticateUser()函数中定义。出于安全目的,我没有在此处包含代码。

问题是按钮位于客户端,PHP脚本驻留在服务器端。任何人都可以告诉我如何实现这一目标? login.php的源代码如下。提前谢谢。

的login.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Login</title>
</head>

<body>
    <?php function authenticateUser($username,$password) { //Code to check the user 's credentials
            }
        ?>
        <form id="form1" name="form1" method="post" action="index.php">
        <input name="txtUsername" type="text" id="txtUsername" size="30" maxlength="100" />
        <input name="txtPassword" type="password" id="txtPassword" size="30" maxlength="100" />
        <input type="submit" name="btnLogin" id="btnLogin" value="Log In" />
        </form>
</body>
</html>

4 个答案:

答案 0 :(得分:3)

使用AJAX请求并分离逻辑,或将表单提交到同一页面,然后进行检查。例如;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
</head>
<body>
<?php 
   if( isset($_POST) ) {
         //Let's assume sanitation and validation on input is done in the function
         authenticateUser($_POST['txtUsername'], $_POST['txtPassword']);
   }
    function authenticateUser($username,$password)
        {
        //Code to check the user's credentials
    }
?>
<form id="form1" name="form1" method="post" action="">
<input name="txtUsername" type="text" id="txtUsername" size="30" maxlength="100" />
<input name="txtPassword" type="password" id="txtPassword" size="30" maxlength="100" />
<input type="submit" name="btnLogin" id="btnLogin" value="Log In" />
</form>

我做了什么;

  • 添加if( isset($_POST) ) {以查看用户是否发布了任何内容,然后调用该函数
  • 删除了表单中的操作属性值。

回答以下评论中的问题

1)我更个人偏好。另外,如果从index.php重命名此页面,您的脚本将无法按预期工作。将其设为空白将发布到当前页面。

2)假设所有检查都已完成,并且您只是想将它们重定向到所述页面,则可以执行以下操作;

ob_clean(); //As we'd be sending a header() request after HTML output.
header('Location: index.php');

如果authenticateUser返回布尔值TRUE,你可以把它放在authenticateUser函数或其他地方 - 这一切都取决于你的逻辑。

此外,请确保为登录用户创建会话并相应地处理会话,同时考虑处理漏洞利用的措施 - 例如会话劫持。

答案 1 :(得分:1)

您将朝着正确的方向前进,您无法在点击时执行该功能。但是,您可以在设置操作上执行该功能,并使用isset()进行测试。

if (isset($_POST['btnLogin'])) {
    authenticateUser($_POST['txtUsername'], $_POST['txtPassword']);
}

如果设置了btnLogin,这将执行该功能,但您需要对该页面发出新请求(即,当您单击该按钮时,该页面将再次加载,然后它将执行该功能。

您可以使用ajax请求来简化请求 - 响应。

但是,当您使用POST数组中的值时,您不需要将它们作为参数传递。它们是全局范围的一部分,因此它们对函数可见。

答案 2 :(得分:0)

简答:无法将提交按钮绑定到PHP函数。

原因很简单:PHP是服务器端,HTML / JS是客户端。客户端根本看不到任何PHP代码。

要解决此问题,您可以使用jQuery将提交按钮绑定到通过Ajax调用PHP函数的函数,然后将PHP代码移动到单独的文件中。

答案 3 :(得分:0)

我会提交到同一页面并检查那里的输入。然后重定向,如果它通过验证。

     <?php
            if(isset($_POST))  //checks if form has been submitted
            {
                authenticateUser($_POST['txtUsername'],$_POST['txtPassword']);
            }

            function authenticateUser($username,$password)
            {
                //Code to check the user's credentials
                //example:
                if($username === null) 
                {
                 header('location:index.php'); //redirects to index.php
                }
            }
        ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Login</title>
        </head>
        <body>
        <form id="form1" name="form1" method="post" action="login.php">
        <input name="txtUsername" type="text" id="txtUsername" size="30" maxlength="100" />
        <input name="txtPassword" type="password" id="txtPassword" size="30" maxlength="100" />
        <input type="submit" name="btnLogin" id="btnLogin" value="Log In" />
        </form>
    </body>
</html>
相关问题