简体   繁体   中英

how to send post data in jquery using ajax

i have a simple form and i want that the as the input field losses the focus the event is fired which will check the content if and replies accordingly to that

$("document").ready(function()
    $("#txt_Email").blur(function() {                   
        var email=$("#txt_Email").val();                   
        $.post('ajax/tester.php', {chk:email}, function(data) {
            alert(data) ;
        });
    });
}); 

so i have done till this but it's showing nothing: no error, no output. i am using jquery 1.9.1 and i also want to know how to check the value of post data on the ajax/tester.php page

Right now i am doing this

$tochk=isset($_POST['chk'])?$_POST['chk']:null;

echo $tochk;

I hope this helps:

$(function(){
    $("#txt_Email").blur(function() {                   
        var email=$("#txt_Email").val(); 
        alert('email: ' + email);
        var jqxhr = $.post('ajax/tester.php', {chk:email});
        jqxhr.done(function(data) { alert(data); });
        jqxhr.fail(function() { alert("error"); });
    });
});

window.onerror = function(errorMessage, url, line) {
    var errorText = 'message: ' + errorMessage + '\nurl: ' + url + '\nline: ' + line;
    alert(errorText);
}

Change this line:

$("document").ready(function()

to:

$(document).ready(function()

You need to refer to the document object, not the string "document" . Using your same code with this small change it works fine. Here's an example: http://jsfiddle.net/YjEMd/ . Note that this example has change the URL of the Ajax and the format of the data being posted so that it can work inside jsFiddle's sandbox. Also the $(document).ready(...) bit is done for you in the background.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM