0

I trying to get data from data base via ajax code. PHP code works well but ajax reponse always is undefined. I tried json bat resuls is same. here are codes:

    var email = $('#email').val();

    function getAjax(p, m) {
        $.ajax({
            url: '../lib/checkmail.php',
            type: 'post',
            data: {val : p, column : m},
            success: function (data) {
                return data.toString();
            }
        });
    }
    if (email.length > 0){

        alert(getAjax(email, 'youremail'));
    }

enter image description here

  • may be issue in checkmail.php file – R P Jun 27 '18 at 06:06
  • Where is your lib folder placed ? Is it placed one level before the file that you are calling the AJAX from ? Based on your screenshot I think you are getting a Notice. Try accessing the same thing in Firefox. Google developer tools will not always give you the complete response. I think if you solve the Notice issue then Your issue will be resolved as well. – Kishen Nagaraju Jun 27 '18 at 06:09

1 Answers1

0

An ajax call works asynchroneously. So when you call getAjax(p, m) if will start to get the url, which takes time, but the function returns immediately. Only after the url has been retrieve will the success function of the ajax call be executed. So what you could test is this:

var email = $('#email').val();

function getAjax(p, m) {
    $.ajax({
        url: '../lib/checkmail.php',
        type: 'post',
        data: {val : p, column : m},
        success: function (data) {
            alert(data.toString());
        }
    });
}
if (email.length > 0){

    getAjax(email, 'youremail');
}

this should give you an alert with the correct response.

Another, obvious, problem is, which you might now realize, that getAjax(p, m) does not return a value, ever. The return belongs to the success function. Hence the return value of getAjax(p, m) will always be 'undefined'.

You will have to learn how to work with an event driven asynchroneous programming language. Better use a good book, or course.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33