0

I have this HTML

<section id="login">
    <form>
        <input type="text" name="username" size="10" placeholder="username" />
        <input type="password" name="password" size="10" placeholder="password" />
        <input type="submit" name="login" value="Login" />
    </form>
</section>

With this $.ajax() that calls the PHP (sys_login.php).

$(function(login){
$('#login form').submit(function(e){

    var uname   =   $('input[name="username"]').val();
    var pwd     =   $('input[name="password"]').val();

    var dataObject = {
            command     : "login",
            username    : uname,
            password    : pwd
        };

    $.ajax({
        type     : "POST",
        url      : "application/sys/sys_login.php",
        data     : dataObject,
        dataType : "json",
        success: function(response, status){
            if (response.status===true){
                docCookies.setItem("sessionid", response.sessionid);
                docCookies.setItem("username", response.username);
                docCookies.setItem("roles", response.roles);
                location.href = "application/index.php";

            }else{
                console.log("false")
                }           
            },
        beforeSend:function(){
            console.log("sending")
        }
    }).error(function() {
        console.log("error");
    });
    e.preventDefault();
});
});

But it never got to the sys_login.php file. If I use GET, it will reach the php file. During debug, even the console didn't print anything. What did I do wrong?

**UPDATE Apparently the backend server malfunctioned. The script works on other machines. Thank you for you time guys.

rolodex
  • 558
  • 1
  • 7
  • 19

3 Answers3

2

You should stringify(dataObject) before sending it to PHP.

Try with

    $.ajax({
        type     : "POST",
        url      : "application/sys/sys_login.php",
        data     : JSON.stringify(dataObject),
        dataType : "json",
)};

see here

Community
  • 1
  • 1
yashhy
  • 2,856
  • 5
  • 31
  • 57
0

Can you try this, Make sure you have unique element Id in your html,

<section >
    <form id="login">
        <input type="text" name="username" size="10" placeholder="username" />
        <input type="password" name="password" size="10" placeholder="password" />
        <input type="submit" name="login" value="Login" />
    </form>
</section>

 <script type="text/javascript">
 $(function(){
        $('#login').submit(function(e){

            var uname   =   $('input[name="username"]').val();
            var pwd     =   $('input[name="password"]').val();

            var dataObject = {
                    command     : "login",
                    username    : uname,
                    password    : pwd
                };

            $.ajax({
                type     : "POST",
                url      : "application/sys/sys_login.php",
                data     : dataObject,
                dataType : "json",
                success: function(response, status){
                    if (response.status===true){
                        docCookies.setItem("sessionid", response.sessionid);
                        docCookies.setItem("username", response.username);
                        docCookies.setItem("roles", response.roles);
                        location.href = "application/index.php";

                    }else{
                        console.log("false");
                        }           
                    },
                beforeSend:function(){
                    console.log("sending");
                }
            }).error(function() {
                console.log("error");
            });
            e.preventDefault();
        });
    });

ref:- http://api.jquery.com/submit/

Krish R
  • 22,583
  • 7
  • 50
  • 59
0

Try this code:

 $(function(login){
    $('#login form').submit(function(e){

        var uname   =   $('input[name="username"]').val();
        var pwd     =   $('input[name="password"]').val();

        var dataObject = '{
                "command"     : "login",
                "username"    : "'+uname+'",
                "password"    : "'+pwd+'"
            }';

        $.ajax({
            type     : "POST",
            url      : "application/sys/sys_login.php",
            data     : dataObject,
            dataType : "json",
            success: function(response){
                if (response.status===true){
                    docCookies.setItem("sessionid", response.sessionid);
                    docCookies.setItem("username", response.username);
                    docCookies.setItem("roles", response.roles);
                    location.href = "application/index.php";

                }else{
                    console.log("false")
                    }           
                },
            beforeSend:function(){
                console.log("sending")
            }
        }).error(function() {
            console.log("error");
        });
        e.preventDefault();
    });
    });