php - Ajax returning undefined from JSON - Login system & best practice -


i'm receiving error of undefined when try , retrieve values in json. i'm new ajax / js etc , trying create 'elegant' drop down login down.

i've tried various things , read few of posts i've found here notice layout has changed , notice i'm using success , deprecated.

so ask in firstly understanding problem , how solve undefined issue , secondly best way achieve this. i'd prefer not use deprecated code if can it.

i've noticed since changing code gets 'success' park of ajax call, drop down box no longer rolls or displays error messages. -.-

thanks in advance.

the ajax

function validlogin(){ $('.error').hide(); var username = $('#username').val(); if(username == ""){     $('label#usernameerror').show();     $('input#username').focus();     return false; } $('.error').hide(); var password = $('#password').val(); if(password == ""){     $('label#passworderror').show();     $('input#password').focus();     return false; }  var params = {username: username, password: password}; var url = "../js/loginprocessajax.php";  $("#statuslogin").show(); $("#statuslogin").fadein(400).html('<img src="images/loading.gif" />');  $.ajax({     type: 'post',     url: url,     data: params,     datatype: 'json',     beforesend: function() {         document.getelementbyid("statuslogin").innerhtml= 'checking...' ;     },      success: function(data) {         alert("success area ofajax");          $("#statuslogin").hide();          if(data.success == true){             alert("if data.success area of ajax");             alert(data.message);          }else{             alert("data.message... " + data.message);//undefined             $("#errorconsole").html(data.message);         }      },     error: function( error ) {         console.log(error);     } }, 'json'); } 

php

<?php  if($_post){   if($users->userexists($username) === false){     $data['message'] = "user doesn't exist";     $data['success'] = false;  }else if($users->userexists($username) === false){     $data['message'] = 'that username not exist';     $data['success'] = false;  }else if($users->emailactivated($username) === false){     $data['message'] = 'you need activate account, please check email.';     $data['success'] = false;  }else{      $login = $users->login($username, $password);      if($login === false){          $data['message'] = 'incorrect password or username';         $data['success'] = false;     }else{         $data['success'] = true;         //destroy old session , create new - prevents session fixation attacks         session_regenerate_id(true);         //all details correct - method returns id sotred session         $_session['id'] = $login;     }      echo json_decode($data); }  } 

markup:

<form method="post" action="" id="ourloginformid_js">                     <div class="ourcontactformelement2">                         <label for="username">username:</label>                         <input type="text" id="username"  name="username" autocomplete="off" class="required" value="<?php if(isset($_post['username'])) echo htmlentities($_post['username']); ?>"  />                     </div>                      <div class="ourcontactformelement2">                         <label for="password">password:</label>                         <input type="password" id="password" name="password" autocomplete="off" class="required"/>                     </div>                      <div class="ourcontactformelement2">                         <label> &nbsp; </label>                         <input type="submit" name="loginbutton" id="loginbutton" value="login!" onclick="validlogin(); return false;"/>                     </div>                      <div id="statuslogin"></div>                 </form> 

your if/else/else/else chain outputs json if final else block executes. need move json_encode call outside block:

if (...) { } else if (...) { } else if (...) { } else {  ... } echo json_encode($data); 

this way code output encoded $data, no matter of various if() clauses executed.


Comments