Webアプリケーションに使用する単純なPHPMVCフレームワークを作成しました。ただし、(jQueryを使用して)AJAXリクエストを実行しようとすると、期待どおりに動作しません。関連するコードの断片は次のとおりです。
意見
<input type="text" name="email" id="email">
<input type="password" name="password" id="password">
<a id="btnlogin">Login</a>
Jquery(別のファイルに配置)
$("#btnlogin").click(function(){
var email = $("#email").val();
var password = $("#password").val();
$.ajax({
type:"POST",
url:"user/login",
data:{email : email, password : password},
cache:false,
success:function(){
alert("suksesssssss");
}
});
コントローラー名:ユーザー
public function login()
{
$email = $_POST['email'];
$password = $_POST['password'];
$this->model->login($email,$password);
}
モデル名:UserModel
public function login($email,$password)
{
$sth = $this->db->prepare("select * from tbl_user where email = :email and password = :password");
$sth->execute(array(
':email' => $email,
':password' => Hash::create('sha256', $password, PASSWORD_HASH_KEY)
));
$count = $sth->rowCount();
if($count > 0)
{
$data = $sth->fetch();
Session::init();
Session::set('id', $data['id']);
Session::set('fullName', $data['nama_lengkap']);
$this->updateLastLogin($data['id']);
$this->updateLoginCount($data['id']);
header('location:' . URL . 'user/profile');
}
else
{
header('location:' . URL . 'home/alogin');
}
}
私の目標は、ログインが成功したときにプロファイルページにリダイレクトすることです。しかし、関数:
header('location:' . URL . 'user/profile');
これは実行されません。
この動作の原因は何ですか?jQueryを使用して別のページにリダイレクトする方が良いですか?