There is an authorization form that sends the post data to the file handler, which in turn processes the data and displays a message to the user in case of an error (incorrect password or login), everything works fine, BUT if the data is correct, you should be redirected to the main page via header, on php, everything works fine, via ajax redirection does not go, only if you restart the page yourself. How to make the redirect work
Html form ...
<form action="/social/system/form.php" method="post" id="login_form"> <table> <tr><td><i class="fa fa-user"></i></td><td><input type="text" name="login" id="login" /></td></tr> <tr><td><i class="fa fa-key"></i></td><td><input type="password" name="uspass" id="pass"/></td></tr> </table> <input type="submit" name="login_user" value="Войти" /> <div id="auth_responce"></div> </form>
Php handler ...
if(isset($_POST['login_user'])) { //(если существует переменная login и эта переменная не пустая) или ... if ((!isset($_POST['login']) or empty($_POST['login'])) or (!isset($_POST['uspass']) or empty($_POST['uspass']))) { header("Location: /index.php"); } else { $params = array($_POST['login'], $_POST['uspass']); $query->Control($params); $query->AuthUser(); } }
The class in which the php handler sends the data for verification and returns a response ...
function AuthUser () { try{ $sql_auth = $this->db->prepare("select id, login, pass, status, lang from users where login = :login and pass = :pass"); $sql_auth->execute(array( 'login' => $this->newParamArray[0], 'pass' => md5($this->newParamArray[1]) )); $sql_auth->setFetchMode(PDO::FETCH_ASSOC); $user_date = $sql_auth->fetch(); if($user_date['login'] == $this->newParamArray[0] and $user_date['pass'] == md5($this->newParamArray[1])) { session_start(); $_SESSION['us_id'] = $user_date['id']; $_SESSION['us_login'] = $user_date['login']; $_SESSION['us_status'] = $user_date['status']; $_SESSION['us_lang'] = $user_date['lang']; header("Location: /index.php"); } else { echo "Простите, но такого пользователя нет или Вы ошиблись при вводе данных! <a href='/index.php'>Попробовать снова</a>"; } } catch (PDOException $e) { echo "Произошла ошибка, смотри файл ошибок."; file_put_contents('system/PDOErrors.txt', $e->getMessage().'<br />', FILE_APPEND); } }
And a java script with ajax that sends data without rebooting ...
$("#login_form").validate( { rules: { login: { required:true, rangelength: [5, 20] }, uspass: { required: true } }, messages: { login: { required: "Разве можно войти без логина?", rangelength: "ну и длиной он должен быть от 5 до 20 см" }, uspass: { required: "Ладно логин, ну а пароль-то обязателен!" } }, submitHandler: function (form) { $.ajax({ type: "POST", url: "/social/system/form.php", data: $(form).serialize(), success: function (msg) { $(form).html("<div id='message'>"+msg+"</div>"); }, //onSuccess() { location.href = index.php; } }); return false; }
});
When the password or login does not match ajax receives an error response and shows it to the user, it works successfully. In the case when the user is authorized, there should be an automatic reload on the index page, everything works without ajax, and with ajax this reboot does not work, how to fix it ??? thank