我正在使用模态框按钮的id向login.php提交值。我的jquery代码是:
$('#login1').on('click', function () {
$.post('login.php', {
'u': $('#email').val(),
'p': $('#password').val(),
'r': $('#role').val()
}, function (data) {
if (data == 'success') {
$('#myModal1').modal('show');
}
else {
$('#myModal2').modal('show');
}
});
return false;
});
的login.php
<?php
$u=$_POST['u'];
$p=$_POST['p'];
$r=$_POST['r'];
$p=md5($p);
$mysqli=new mysqli('localhost','root','password','user_details');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$q=$mysqli->query("select * from register_info where Email='{$u}' , Password='{$p}' and Role='{$r}'");
$e=$q->num_rows;
if($e>0)
{
$loggedIn=true;
$result='success';
$_SESSION['logged_in']=true;
header('location: profile.php?email='.$u);
}
else
{
$loggedIn=false;
$result='fail';
}
?>
模态表单代码:
<div class="modal-body">
<form class="form-horizontal" action='login.php' method="POST">
<br>
<fieldset>
<div id="legend">
<legend class="">Login Details</legend>
</div>
<div class="control-group">
<!-- Username -->
<label class="control-label" for="email">Email</label>
<div class="controls">
<input type="text" id="email" name="email" placeholder="Enter Email" class="input-xlarge">
</div>
</div>
<div class="control-group">
<!-- Password-->
<label class="control-label" for="password">Password</label>
<div class="controls">
<input type="password" id="password" name="password" placeholder="Enter Password" class="input-xlarge">
</div>
</div>
<div class="control-group">
<!-- Password-->
<label class="control-label" for="role">Role</label>
<div class="controls">
<select name="role" class="form-control input-sm" id="role" placeholder="Enter role">
</select>
</div>
</div>
<br>
<br>
<button type="button" class="btn btn-success btn-lg" id="login1" data-dismiss="modal">Login</button>
</fieldset>
</form>
</div>
</div>
<div class="modal-footer">
</div>
</div>
我希望当我点击登录模式框的按钮成功检查电子邮件和密码时,它应该将我重定向到profile.php页面。请帮我纠正代码
答案 0 :(得分:4)
您尝试将数据从表单发送到数据库。您的PHP代码没有返回值。用户登录时重定向到另一个页面。因此我修改了可以帮助你的PHP代码...
<?php
$u=$_POST['u'];
$p=$_POST['p'];
$r=$_POST['r'];
$p=md5($p);
$mysqli=new mysqli('localhost','root','password','user_details');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$q=$mysqli->query("select * from register_info where Email='{$u}' , Password='{$p}' and Role='{$r}'");
$e=$q->num_rows;
if($e>0)
{
$loggedIn=true;
$result='success';
$_SESSION['logged_in']=true;
}
else
{
$loggedIn=false;
$result='fail';
}
echo $result; // based on this open your modal window
?>
答案 1 :(得分:1)
您在SQL查询中遇到一个问题。
另一个问题是你的jQuery需要遵循ajax post响应的重定向,以下是未经测试的代码:
$('#login1').on('click', function () {
$.post('login.php', {
'u': $('#email').val(),
'p': $('#password').val(),
'r': $('#role').val()
}, function (e, xhr) {
location.href = '/profile.php';
});
return false;
});