【发布时间】:2014-01-11 12:52:42
【问题描述】:
我是 PHP 新手,刚刚开始学习这门语言的基础知识。
我创建了一个页面login.php,其中包含一个文本字段和一个密码字段。我想要做的是,在单击提交按钮时,我想调用我在同一页面上定义的 PHP 方法 authenticateUser()。如果用户成功通过身份验证,那么他将被定向到 index.html 页面。如果不是,那么他会留在同一页面上,并且会弹出一条错误消息,指出用户名/密码不正确。所有这些逻辑都在authenticateUser() 函数中定义。出于安全考虑,我没有在此处包含代码。
问题在于按钮位于客户端,而 PHP 脚本位于服务器端。谁能告诉我如何实现这一目标? login.php 的源代码如下。提前谢谢你。
登录.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>
【问题讨论】:
-
你应该阅读关于 Ajax 的文档。