1

I don't know what's going on, I want to pass the data from html input to my php file using ajax. i have two fields which is username and password. here's my code in ajax.

$(document).ready(function () {
    //initialize the javascript
    function search() {
        var username = $("#username").val();
        var password = $("#password").val();
        var dataString = 'username=' + username + '&password=' + password;
        if (username != "") {
            $.ajax({
                type: "POST",
                url: "ajax.php",
                data: dataString,
                cache: false,
                beforeSend: function () {
                    $("#login").html("<img style='width: 20px;' alt='loading...' src='ajax-loader.gif'/>");
                },
                success: function (data) {
                    if (data == 1) {
                        window.location = "management/dashboard";
                    } else {
                        $("#login").html(data);
                    }
                }
            })
            return false;
        } else {
            $("#login").html('login');
        }
    };
    $('#username').keyup(function (e) {
        if (e.keyCode == 13) {
            search();
        }
    });
    $('#password').keyup(function (e) {
        if (e.keyCode == 13) {
            search();
        }
    });
    $('#login').click(function () {
        search();
    });

});

and this is my php code..

<?php

    if(!empty($_POST['username']) && !empty($_POST['password'])) {
        echo 1;
    } else {
        echo 0;
    }

?>

Thanks in advance.

2
  • What's the problem/error?
    – michelem
    Sep 8, 2015 at 7:01
  • undefined index username and password
    – Gcode
    Sep 8, 2015 at 7:30

1 Answer 1

2

change this:

$.ajax({
    type: "POST",
    url: "ajax.php",                        
    data: dataString,
    cache: false,
    beforeSend: function(){ 
        $("#login").html("<img style='width: 20px;' alt='loading...' src='ajax-loader.gif'/>"); 
    },                      
    success: function(data){
        if(data == 1) {
            window.location="management/dashboard";
        }
        else {
            $("#login").html(data);
        }
    }               
})

to this:

$.ajax({
    type: "POST",
    url: "ajax.php",                        
    data:{username: username, password:password},
    cache: false,
    beforeSend: function(){ 
        $("#login").html("<img style='width: 20px;' alt='loading...' src='ajax-loader.gif'/>"); 
    },                      
    success: function(data){
        if(data == 1) {
            window.location="management/dashboard";
        }
        else {
            $("#login").html(data);
        }
    }                   

})

The problem was in sending parameters. You sended parameters in GET mode, instead of POST mode:

data:{username: username, password:password}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.