简体   繁体   中英

Submitting form via ajax in jquery

i am having some problems with getting my form to submit. It doesnt seem like anything is being send, is their anything wrong with this code as javascripting isn't my strong point...

$("#send").click(function() {
        var complete = true;
        $('input#name, input#email, input#subject, textarea#message').each(function() {
            if ($(this).val()) {
                $(this).css("background","#ffffff").css("color","#5c5c5c");
            } else {
                $(this).css("background","#d02624").css("color","#ffffff");
                complete = false;
            }
        });
        if (complete == true){
            var name = $("input#name").val();
            var email = $("input#email").val();
            var subject = $("input#subject").val();
            var message = $("textarea#message").val();
            var data = '{"name":"'+name+'","sender":"'+email+'","subject":"'+subject+'","message":"'+message+'"}';
            $.ajax({
                type:"POST",  
                url:"contact.php",  
                data:$.base64.encode(data),
                success:function(data){
                    data = $.parseJSON(data);
                    if (data.status == "success") {
                        $.fancybox.close();
                    }
                }
            });
        }
    });

There is also a live version of this in action which can be viewed over at: http://idify.co.uk , thanks :)

You can do it better.

$('form')
    .submit(function(event) {
        var form = $(this);
        $.ajax({
            url: '[url here]',
            type: 'post',
            data: $.base64.encode(form.serialize()),  // $.serialize() - it gets all data from your form
            dataType: 'json', // function in success callback knows how to parse returned data
            success: function(data) {
                if (data['status'] == true) {
                    // your code here
                    // e.g.
                    $.fancybox.close();
                }
            }
        });
        event.preventDefault();
    });

Enjoy! :)

I got an error after submitting:

data is null http://idify.co.uk/javascripts/landing.js Line 25

It looks like the data was sent successfully and there was a response:

 {"status":"error","responce":"No token parameter was specified."} 

This should help you ensure you've got data in your success callback:

            success:function(response) {
                if (response) {
                    var data = $.parseJSON(response);
                    if (data && data.status == "success") {
                        $.fancybox.close();
                    }
                } else {
                    // handle errors
                }
            }

Haha, thanks guys. I was silly enough not to include the variable that needs to be passed via the php file, got it sorted and it works like a dream, i ended up using the first solution as the form submission one wasnt working for me.

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