Php 用户名和密码通过ajax登录。。无法登录
控制器代码如下所示,无法登录。 没有一个错误是我无法理解如何通过ajax调用的问题Php 用户名和密码通过ajax登录。。无法登录,php,ajax,Php,Ajax,控制器代码如下所示,无法登录。 没有一个错误是我无法理解如何通过ajax调用的问题 public function loginn() { $email=$this->input->post('email'); $password=$this->input->post('password'); //$this->load->l
public function loginn()
{
$email=$this->input->post('email');
$password=$this->input->post('password');
//$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email or number', 'required|min_length[10]|max_length[30]');
$this->form_validation->set_rules('password', 'password', 'trim|required|min_length[4]|max_length[40]');
if ($this->form_validation->run() && $this->Login_model->login($email, $password)) {
$this->welcome();
}
else {
$this->form_validation->set_message('check_database', 'Invalid username or password');
$this->index();
}
}
下面的代码如图所示查看页面通过ajax发送post值。脚本如下。如何使用ajax将post值传递给控制器
<script type="text/javascript">
$(document).ready(function(){
$("#fpassf").click(function(){
//e.preventDefault();
var email = $("#email").val();
var password= $("#password").val();
$.ajax({
type: "POST",
url: "<?php echo base_url() ?>"+"Index.php/Login_cntrl/loginn",
data: {email:email,password:password},
success:function(data)
{
alert('Successfully login');
},
error:function()
{
alert('fail');
}
});
});
});
$(文档).ready(函数(){
$(“#fpassf”)。单击(函数(){
//e、 预防默认值();
var email=$(“#email”).val();
var password=$(“#password”).val();
$.ajax({
类型:“POST”,
url:“+”Index.php/Login\u cntrl/loginn”,
数据:{电子邮件:电子邮件,密码:密码},
成功:功能(数据)
{
警报(“成功登录”);
},
错误:函数()
{
警报(“失败”);
}
});
});
});
如下图所示 单击下面显示的登录图像
您似乎正在使用Codeigniter框架。因此,我对您的Ajax URL路径有疑问 你已经放弃了
url: "<?php echo base_url() ?>"+"Index.php/Login_cntrl/loginn",
看起来您正在使用Codeigniter框架。因此,我对您的Ajax URL路径有疑问 你已经放弃了
url: "<?php echo base_url() ?>"+"Index.php/Login_cntrl/loginn",
您必须将基本登录操作和ajax登录操作分开,因为两者都发送不同的响应 PHP控制器:
public function ajax_login()
{
$email = $this->input->post('email');
$password = $this->input->post('password');
$this->form_validation->set_rules('email', 'Email or number', 'required|min_length[10]|max_length[30]');
$this->form_validation->set_rules('password', 'password', 'trim|required|min_length[4]|max_length[40]');
if ($this->form_validation->run() && $this->Login_model->login($email, $password)) {
return $this
->output
->set_status_header(200)
// here you tell to the ajax that the login is successful
->set_output(json_decode(array('status' => 'success', 'message' => 'PUT_YOUR_SUCCESS_MESSAGE_HERE')))
;
} else {
return $this
->output
->set_status_header(200)
// here you tell to the ajax that the login is failed
->set_output(json_decode(array('status' => 'error', 'message' => 'PUT_YOUR_ERROR_MESSAGE_HERE')))
;
}
}
Javascript:
<script type="text/javascript">
$(document).ready(function(){
$("#fpassf").click(function(){
var email = $("#email").val();
var password = $("#password").val();
$.ajax({
type: "POST",
url: "<?php echo base_url('Login_cntrl/ajax_login') ?>",
data: {email:email, password:password},
success: function(data) {
data = JSON.parse(data);
// success in ajax does not mean successfully login, so check again
if (data.status == 'success') {
alert(data.message]); // login success message defined in action
} else {
alert(data.message); // login failed message defined in action
}
},
error: function() {
// error in ajax means HTTP error, not login error
alert('Request failed!');
}
});
});
});
</script>
$(文档).ready(函数(){
$(“#fpassf”)。单击(函数(){
var email=$(“#email”).val();
var password=$(“#password”).val();
$.ajax({
类型:“POST”,
url:“”,
数据:{电子邮件:电子邮件,密码:密码},
成功:功能(数据){
data=JSON.parse(数据);
//ajax中的成功并不意味着成功登录,请再次检查
如果(data.status==“成功”){
警报(data.message));//在操作中定义了登录成功消息
}否则{
警报(data.message);//在操作中定义的登录失败消息
}
},
错误:函数(){
//ajax中的错误意味着HTTP错误,而不是登录错误
警报('请求失败!');
}
});
});
});
您必须将基本登录操作和ajax登录操作分开,因为两者发送的响应不同
PHP控制器:
public function ajax_login()
{
$email = $this->input->post('email');
$password = $this->input->post('password');
$this->form_validation->set_rules('email', 'Email or number', 'required|min_length[10]|max_length[30]');
$this->form_validation->set_rules('password', 'password', 'trim|required|min_length[4]|max_length[40]');
if ($this->form_validation->run() && $this->Login_model->login($email, $password)) {
return $this
->output
->set_status_header(200)
// here you tell to the ajax that the login is successful
->set_output(json_decode(array('status' => 'success', 'message' => 'PUT_YOUR_SUCCESS_MESSAGE_HERE')))
;
} else {
return $this
->output
->set_status_header(200)
// here you tell to the ajax that the login is failed
->set_output(json_decode(array('status' => 'error', 'message' => 'PUT_YOUR_ERROR_MESSAGE_HERE')))
;
}
}
Javascript:
<script type="text/javascript">
$(document).ready(function(){
$("#fpassf").click(function(){
var email = $("#email").val();
var password = $("#password").val();
$.ajax({
type: "POST",
url: "<?php echo base_url('Login_cntrl/ajax_login') ?>",
data: {email:email, password:password},
success: function(data) {
data = JSON.parse(data);
// success in ajax does not mean successfully login, so check again
if (data.status == 'success') {
alert(data.message]); // login success message defined in action
} else {
alert(data.message); // login failed message defined in action
}
},
error: function() {
// error in ajax means HTTP error, not login error
alert('Request failed!');
}
});
});
});
</script>
$(文档).ready(函数(){
$(“#fpassf”)。单击(函数(){
var email=$(“#email”).val();
var password=$(“#password”).val();
$.ajax({
类型:“POST”,
url:“”,
数据:{电子邮件:电子邮件,密码:密码},
成功:功能(数据){
data=JSON.parse(数据);
//ajax中的成功并不意味着成功登录,请再次检查
如果(data.status==“成功”){
警报(data.message));//在操作中定义了登录成功消息
}否则{
警报(data.message);//在操作中定义的登录失败消息
}
},
错误:函数(){
//ajax中的错误意味着HTTP错误,而不是登录错误
警报('请求失败!');
}
});
});
});
输入jquery代码有什么问题?错误?问题?预期与实际行为?由于缺少详细信息,投票关闭。如果有任何问题,请检查AJAX响应和PHP错误。也许这会帮助你找出问题所在。你真的有这个url吗url:“+”Index.php/Login\u cntrl/loginn“,`检查它,与您的浏览器一起是的,我有那个url放上您的jquery代码有什么问题?错误?问题?预期与实际行为?由于缺少详细信息,投票关闭。如果有任何问题,请检查AJAX响应和PHP错误。也许这会帮助你找出问题所在。你真的有这个url吗url:“+”Index.php/Login\u cntrl/loginn,`检查一下,和你一起浏览是的,我有那个url
<script type="text/javascript">
$(document).ready(function(){
$("#fpassf").click(function(){
var email = $("#email").val();
var password = $("#password").val();
$.ajax({
type: "POST",
url: "<?php echo base_url('Login_cntrl/ajax_login') ?>",
data: {email:email, password:password},
success: function(data) {
data = JSON.parse(data);
// success in ajax does not mean successfully login, so check again
if (data.status == 'success') {
alert(data.message]); // login success message defined in action
} else {
alert(data.message); // login failed message defined in action
}
},
error: function() {
// error in ajax means HTTP error, not login error
alert('Request failed!');
}
});
});
});
</script>