0

I want to do this simple log in task using php web service. I am just trying to authenticate username and password on the basis of text result I am echoing in my 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 * 
                                           from `login` 
                                           where `email`='$email' 
                                           and `password`='$password'"));
        $result =array();
        if($login!=0)
    {
    echo "success";
    }
else
{
echo "failed";
}
}
    @mysql_close($conn);

    /* Output header */
    header('Content-type: text/plain');


?>

If the username and password match; it 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 are in");
                                localStorage.login = "true";
                                localStorage.email = email;
                                window.location.href = "test.html";   
                            }
                            else if (data == "failed") {
                                alert("Login error");
                                $("#logon").html('Login');
                            }  
                        }
                    });
                }
            });
        });
    </script>
EJW
  • 338
  • 3
  • 6
  • 18
  • What is the output when you print returned string like `alert(data);` ? – Aycan Yaşıt Jan 16 '16 at 11:58
  • No output. It is not evening getting to that point. :/ – Abraham Guttman Jan 16 '16 at 12:07
  • U missed datatype html in ajax – devpro Jan 16 '16 at 12:26
  • @devpro – `dataType` is optional. If you omiti it, jQuery will just use the Content-Type response header. – Quentin Jan 16 '16 at 17:35
  • You have `crossDomain: true,` (which probably doesn't do what you think it does) and an absolute URI. Are you making a cross origin request? Have you looked at the Console in your browser's developer tools? Don't you have an error message complaining about the missing Access-Control-Allow-Origin header? – Quentin Jan 16 '16 at 17:36
  • @quentin ... ohh thanks I dont knw about that – devpro Jan 16 '16 at 17:47
  • @Quentin Yes I do see the error as : XMLHttpRequest cannot load http://###/###/login.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:37101' is therefore not allowed access. Though I even disabled crossDomain. – Abraham Guttman Jan 17 '16 at 14:51
  • @AbrahamGuttman — crossDomain does not do what you imagine it does, all it does it remove certain headers that get added to same origin requests in case you make a same origin request which gets redirected to a different origin. – Quentin Jan 17 '16 at 14:54
  • Thank you for explaining this to me. Would you also help me in getting rid of this error? What changes do I have to make in PHP. I just wanted it simple that's why returning simple text. It is my first time using PHP service. – Abraham Guttman Jan 17 '16 at 15:06
  • Result variable in ajax succeas undefined – devpro Jan 17 '16 at 19:57
  • @quentin I think result variable in ajax success is the issue an umdefine variable – devpro Jan 17 '16 at 20:23
  • @AbrahamGuttman — See the duplicate question. – Quentin Jan 17 '16 at 20:31
  • @devpro — The result variable isn't undefined, the success function never fires in the first place. – Quentin Jan 17 '16 at 20:32

1 Answers1

-1

you are missing the json function you have to encode what ever you want to send to back to request

   <?php
    /*output the header here*/
     header("Content-Type: application/json");

            // Include confi.php
              include_once('confi.php');
              $response['Status'];//      declare a variable to 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 * 
                                       from `login` 
                                       where `email`='$email' 
                                       and `password`='$password'"));
                $result =array();
               if($login!=0)
        {
           $response['Status']=""success";
        }
     else
        {
            $response['Status']="failed";
         }
       }
@mysql_close($conn);

here make the change

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

?>

in you jquery data to ajax function add

            dataType:"json",
        success: function (data) {
               if (data.Status == "success") {    //here check the condition
                            alert(result+"You are in");
                            localStorage.login = "true";
                            localStorage.email = email;
                            window.location.href = "test.html";   
                        }
              else if (data.Status== "failed") { //here check the condition
                            alert("Login error");
                            $("#logon").html('Login');
                        }  
                    }
Nauman Ahmad
  • 320
  • 4
  • 17
  • The PHP in the question says it is sending back plain text. Nothing in the JavaScript suggests that it is expecting JSON. jQuery will parse the plain text as plain text, which is fine. – Quentin Jan 16 '16 at 17:34
  • in this case it doesn't matter what it return the same functionally is achieved either returning html or json – Nauman Ahmad Jan 16 '16 at 17:47