我已经创建了一个登录页面,但它无效。 我的HTML是:
<form name="form" class="login-form" method="POST">
<input type="text" placeholder="username" name="userID" id="userID"/>
<input type="password" placeholder="password" id="password" name="password"/>
<input type="button" id="login" name="login" value="login" onclick="redirect()"></input>
我的JS是:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function redirect(){
$("#login").click(function(){
var user = $('#userID').val();
var passwrd = $('#password').val();
if (user === '' || passwrd === ''){
alert("Please Fill Required Fields");
}
else{
<?php confirm();?>
var userID="<?php echo $username?>";
var pwd="<?php echo $pwd?>";
if(userID===user && pwd===passwrd)
{
window.location.href = 'http://localhost/Annapoorna/Welcome_Page.php';
}
else
{
alert("Please enter correct username and password!");
}
}
});
});
</script>
PHP就是:
<?php
function confirm(){
try{
$username="";
$pwd="";
$pword="";
$servername = "localhost";
$conusername = "root";
$conpassword = "";
$dbname = "annapoorna";
$dsn='mysql:dbname='.$dbname.';host='.$servername;
$conn = new PDO($dsn, $conusername, $conpassword);
$get_pass=$_POST(['password']);
if(!empty($get_pass))
{
$sqlselect='Select * from users where password=:pass';
$sth=$conn->prepare($sqlselect);
$sth->execute(array(':pass'=>$get_pass));
foreach ($sth->fetchAll() as $row) {
$username=$row['username'];
$pwd=$row['password'];
}
}
}
catch(PDOException $e){
echo "Error: " . $e->getMessage();
}}
?>
它仅适用于针对':pass'的查询中放置的静态密码值。为什么它不能动态工作? 所以我认为问题在于从前端获取数据。 祈祷,告诉我什么邪灵在我的代码上盘旋。
答案 0 :(得分:1)
你可以这样使用
<form name="form" class="login-form" method="POST">
<input type="text" placeholder="username" name="userID" id="userID"/>
<input type="password" placeholder="password" id="password" name="password"/>
<input type="button" id="login" name="login" value="login" onclick="redirect()"></input>
</form>
<script type="text/javascript">
$(document).ready(function redirect(){
$("#login").click(function(){
var user = $('#userID').val();
var passwrd = $('#password').val();
if (user === '' || passwrd === ''){
alert("Please Fill Required Fields");
}
else {
$.ajax({
url: "validate.php",
type: "post",
data : {
username: user,
password: passwrd
},
success: function(data){
alert(data);
if(data=="ok"){
window.location.href="http://localhost/Annapoorna/Welcome_Page.php";
}
}
});
}
});
});
</script>
和validate.php文件
<?php
$servername = "localhost";
$conusername = "root";
$conpassword = "";
$dbname = "annapoorna";
$dsn='mysql:dbname='.$dbname.';host='.$servername;
$conn = new PDO($dsn, $conusername, $conpassword);
$username=$_POST['username'];
$password=$_POST['password'];
$sqlselect="Select * from users where username='".$username."' and password='".$password."'";
$sth=$conn->prepare($sqlselect);
$sth->execute();
$status="";
foreach ($sth->fetchAll() as $row) {
$status="ok";
}
echo $status;
?>
答案 1 :(得分:0)
使用js或jquery提交表单。尝试更新您的代码,如下所示
if(userID===user && pwd===passwrd)
{
document.getElementById("formid").submit();
}
在php中,删除大括号,更改
$get_pass=$_POST(['password']);
到
$get_pass=$_POST['password'];