php - Returning AJAX Success and Error -


i have built login script uses ajax submit form data.

the php part works fine without ajax. system doesnt work ajax implementation.

it displays below message though php file returns true[correct username & password] ... seems if condition in jquery not working.

incorrect username/password

html result div

<div id="user-result" align="center"></div> 

jquery

<script type="text/javascript">     $(document).ready(function () {         var form = $('#loginform');         form.submit(function (ev) {             ev.preventdefault();             $.ajax({                 type: form.attr('method'),                 url: form.attr('action'),                 cache: false,                 data: form.serialize(),                 success: function (data) {                     if (data == "true") {                         $("#user-result").html("<font color ='#006600'> logged in | redirecting..</font>").show("fast");                         settimeout(                             function () {                                 window.location.replace("index.php");                             }, 1500);                     } else {                         $("#user-result").html("<font color ='red'> incorrect username/password</font>").show("fast");                     }                  }              });         });     });  </script> 

fn_login.php

<?php {     session_start();     include_once 'db_connect.php';      if (isset($_post))      {         $email = filter_input(input_post, 'email', filter_sanitize_string);         $logpwd = filter_input(input_post, 'password', filter_sanitize_string);          $stmt = $conn->prepare("select password manager email = ? limit 1");         $stmt->bind_param("s", $email);           $stmt->execute();         $stmt->store_result();          // variables result.         $stmt->bind_result($password);         $stmt->fetch();           // check if user has provided correct password comparing typed our hash         if (password_verify($logpwd, $password))         {             $sql = "select * manager email '{$email}' limit 1";             $result = $conn->query($sql);             $row=mysqli_fetch_array($result);             $id = $row['id'];             $conn->query("update manager set lastlogin = now() id = $id");              $_session['manager_check'] = 1;             $_session['email'] = $row['email'];             $_session['fullname'] = $row['fullname'];             $_session['designation'] = $row['designation'];             $_session['id'] = $row['id'];             echo "true";         }          else {             die();         }      } }  ?>       

can please point out mistake in code/practice.

edit 

just tried disabling ajax, php file works correctly echoing true when username/pass correct

you have spaces after ?>

so, ajax response having spaces after true.

solution:

remove ?> end of php file.

it not affect php functionality.

and ajax response without spaces.

excluding closing tag ?> end of php file standard practice modern php frameworks , cmss.

tips debugging ajax:

1) use firefox (with firebug add) on or chrome.

2) use console tab of firebug, check ajax requests going.

3) here, can see input parameters, headers , important response.

4) so, in short can debug whole ajax request life cycle.


Comments