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

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

Use location.href = url , redirects the user to the desired page.

In the ajax request, execute this line if the user has successfully logged in.

 ... onSuccess() { location.href = url; // ваш url } ... 

UPD
You have a f-tion - success , right there and check what came from the server - an error or everything is fine. success will be executed when the server responds if the статус 200 OK . So make changes to your function success .

 submitHandler: function (form) { $.ajax({ type: "POST", url: "/social/system/form.php", data: $(form).serialize(), success: function (msg) { /** вот тут надо проверить что пришло с нашего сервера */ if(msg == 'success') location.href = url; else $(form).html("<div id='message'>"+msg+"</div>"); }, }); return false; } 

And in php where your session starts, display echo success

 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']; echo "success"; // header("Location: /index.php"); } 

Something like this, but you can still work on the messages that the server sends, so that they show more clearly - the error returned, or everything is ok

  • I can't ... Now I'll show the codes ... - alexandr.patrachi
  • Can you please tell me what corrections should be? - alexandr.patrachi
  • Yes, but in this case, when java is disabled in the browser, php will not redirect you. The main task is to make authorization and verification happen via ajax and php if javascript is disabled in the browser. - alexandr.patrachi