javascript - How to handle an error on ajax login form? -
i have several login forms use ajax, when information wrong page refreshes. want catch keyword/variable php, right echoing 'error', , want alert on page no refresh. here's current js. when submit, login object created , login method performed, if successful redirects dashboard if not echos 'error' , how can accomplish need javascript?
<script> $('#doctor_login').on("submit", function(e){ frmreg = document.getelementbyid("doctor_login"); if(frmreg.user_name.value == "") { alert("<?php echo _username_empty_alert; ?>"); frmreg.user_name.focus(); return false; }else if(frmreg.password.value == ""){ alert("<?php echo _password_is_empty; ?>"); frmreg.password.focus(); return false; }else{ $.ajax({ type:'post', url: '../page/handler/handler_ajax_login.php', data: $(this).serialize() } }); } });
jquery exposes error
callback on $.ajax
method, documented on http://api.jquery.com/jquery.ajax/
$.ajax({ type:'post', url: '../page/handler/handler_ajax_login.php', data: $(this).serialize(), error: function(jqxhr, textstatus, errorthrown) { console.log(textstatus, errorthrown); } }
to trigger error callback, must return 400
or 500
range http code.
<?php header("http/1.1 400 bad request");
Comments
Post a Comment