How to return simple text from php web service using ajax and jquery -


i want simple log in task using php web service. trying authenticate username , password on basis of text result echoing in php.

php:

<?php     // include confi.php     include_once('confi.php');      $found = false;     $email = isset($_post['email']) ? mysql_real_escape_string($_post['email']) : "";     $password = isset($_post['password']) ? mysql_real_escape_string($_post['password']) : "";     if(!empty($email) && !empty($password)){         $login=mysql_num_rows(mysql_query("select *                                             `login`                                             `email`='$email'                                             , `password`='$password'"));         $result =array();         if($login!=0)     {     echo "success";     } else { echo "failed"; } }     @mysql_close($conn);      /* output header */     header('content-type: text/plain');   ?> 

if username , password match; displays success.

jquery

 <script>          $(function () {              $("#logon").click(function () {                 var email = $("#username").val();                 var password = $("#pass").val();                 var datastring = "email=" + email + "&password=" + password;                 if ($.trim(email).length > 0 & $.trim(password).length > 0) {                     $.ajax({                         type: "post",                         url: "http://*****/login.php",                         data:datastring,                         crossdomain: true,                         cache: false,                         beforesend: function () { $("#logon").html('connecting...'); },                         success: function (data) {                             if (data == "success") {                                 alert(result+"you in");                                 localstorage.login = "true";                                 localstorage.email = email;                                 window.location.href = "test.html";                                }                             else if (data == "failed") {                                 alert("login error");                                 $("#logon").html('login');                             }                           }                     });                 }             });         });     </script> 

you missing json function have encode ever want send request

   <?php     /*output header here*/      header("content-type: application/json");              // include confi.php               include_once('confi.php');               $response['status'];//      declare variable store msg               $found = false;               $email = isset($_post['email']) ?               mysql_real_escape_string($_post['email']) : "";                    $password = isset($_post['password']) ?               mysql_real_escape_string($_post['password']) : "";               if(!empty($email) && !empty($password)){            $login=mysql_num_rows(mysql_query("select *                                         `login`                                         `email`='$email'                                         , `password`='$password'"));                 $result =array();                if($login!=0)         {            $response['status']=""success";         }      else         {             $response['status']="failed";          }        } @mysql_close($conn); 

here make change

 $result= json_encode($message);   echo $result; 

?>

in jquery data ajax function add

            datatype:"json",         success: function (data) {                if (data.status == "success") {    //here check condition                             alert(result+"you in");                             localstorage.login = "true";                             localstorage.email = email;                             window.location.href = "test.html";                            }               else if (data.status== "failed") { //here check condition                             alert("login error");                             $("#logon").html('login');                         }                       } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -