I can not understand why Success does not work. (I'm just learning) Authorization passes normally if I write the correct data, but Alert does not show. And if I'm writing too, it does not work.

JQUERY

$(document).ready(function(){ $('#logbutton').click(function(){ var username = $('#user').val(); var password = $('#pass').val(); var login = 'login'; if(username != '' && password != ''){ $.ajax({ url:"index.php", method:"POST", data:{username:username, password:password, login:login}, success:function(res){ if(res == "ok"){ alert('successfully logged in') } } }) } else{ alert('All Fields are required!'); } }) }) 

PHP Code

  if(isset($_POST['login'])){ $user = $_POST['username']; $pass = $_POST['password']; $sql = mysql_query("SELECT * FROM players WHERE username = '".$user."' AND password = '".$pass."'"); if(mysql_num_rows($sql) == 1){ $array = mysql_fetch_array($sql); $_SESSION['user'] = $array['id']; echo "ok"; }else{ $loginerror = "<div class='alert alert-warning'>Invalid Login information.Please return to the <a href='/Artur/'>previous page</a></div>"; } } 
  • look in the error browser console - G.Denis
  • @Pyramidhead, sorry, did not understand what? - TsKinGT
  • @ G.Denis, there are no errors in the console - TsKinGT
  • @TsKinGT, add the error handler, complete , done and see what will be called from this. - Pyramidhead
  • @Pyramidhead is not called. - TsKinGT

2 answers 2

Well, in the above code, at least he doesn’t know what to do if the login has been scanned. Neither from the client, nor from the server.

Rural option: Either:

 if(res == "ok"){ alert('successfully logged in'); }else{ alert('some fail here'); } 

Or

 //php ... }else{ $loginerror = "Invalid Login information.Please return to the previous pagе"; echo $loginerror; //Ну не просто так же присваивать переменной такой длинный текст? Все же выведем его } //js ... if(res == "ok"){ alert('successfully logged in'); }else{ alert(res); // Дабы мы не зря выводили текст с сервера, передадим его пользователю } 

More meaningful option:

 //php ... if( `успешные условия логина` ){ $response[status] = 1; }else{ $response[error] = 'Some error';//в зависимости от того, что стало причиной ошибки - формируем текст ошибки } echo json_encode($response); //js ... success:function(data){ var response = JSON.parse(data); if(response.status){ alert('You have been successfully logged in.'); }else{ alert(response.error); } 
  • still does not work. always work else. - TsKinGT
  • Which option, and which option else - SLy_huh
  • Option 2, always works in successe else .. that is, always when you enter the correct one and when you enter the wrong login and password, an alert is displayed with all HTML tags + at the very top of OK or BUT - TsKinGT
  • What kind of HO? Well, if he alerts along with OK, then see why your server throws both OK and Error at the same time. - SLy_huh 2:41 pm
  • look i did so. success: function (data) {if (data == "ok") {window.location.replace ('home.php')} else {alert (data); }} and in PHP, when the infa is incorrect, echo "no" is done. Here, if I enter the info correctly, the alert pops up with the content of the type: OK and below all my tags. and if you enter the wrong information, the same alert pops up just above it says NO - TsKinGT
 success:function(res){ if(res == "ok"){ alert('successfully logged in') } 

Your php script doesn’t return OK. Sorry what's off topic, but maybe it would be worth starting with a simpler example of learning ajax? JQuery's $ .ajax () Function How to Use