tags:

views:

444

answers:

3

Could someone please give me a hand and tell me what is wrong with this script? The answer that was posted did not work. What I get when I use that code is the entire html page being submitted. This is what I can see from the console. Can someone please help me out? Thanks.


$(function() {
    $('#add_customer_form').submit(function() {
        var data = $(this).serialize();
        var url = $(this).attr('action');
        var method = $(this).attr('method');
        $.ajax({
            url: url,
            type: method,
            data: data,
            dataType: 'json',
            success: function(data) {
                var $div = $('<div>').attr('id', 'message').html(data.message);
                if(data.success == 0) {
                    $div.addClass('error');
                } else {
                    // START CHANGE
                    // you need to get `datastring` from somewhere
                    $.ajax({
                       type: "POST",
                       url: "body.php?action=admCustomer",
                       data: dataString,
                       success: function(){
                           $('#contact input[type=text]').val('');
                           $div.addClass('success');
                       }
                    });
                    // END CHANGE
                }
                $('body').append($div);
            }
        });
        return false;
    });
});
A: 
$(function() {
    $('#add_customer_form').submit(function() {
        var data = $(this).serialize();
        var url = $(this).attr('action');
        var method = $(this).attr('method');
        $.ajax({
            url: url,
            type: method,
            data: data,
            dataType: 'json',
            success: function(data) {
                var $div = $('<div>').attr('id', 'message').html(data.message);
                if(data.success == 0) {
                    $div.addClass('error');
                } else {
                    // START CHANGE
                    // you need to get `datastring` from somewhere
                    $.ajax({
                       type: "POST",
                       url: "body.php?action=admCustomer",
                       data: dataString,
                       success: function(){
                           $('#contact input[type=text]').val('');
                           $div.addClass('success');
                       }
                    });
                    // END CHANGE
                }
                $('body').append($div);
            }
        });
        return false;
    });
});
Piskvor
Hi Piskvor. Thanks for the help! Where you have your comment:"// you need to get `datastring` from somewhere"My datastring is actually above the starting $(function(). Is this ok or does the datastring var need to be inside the else block where your comment is?Thanks for helping me.
Hmm, in that case it's most probably in the global scope and should be available inside your function.
Piskvor
Ok, thanks.. I will give that a try.
+1  A: 

Perhaps changing

var $div = $('<div>').attr('id', 'message').html(data.message);

to

var $div = $('<div/>').attr('id', 'message').html(data.message);

would do the trick?

snz3
Hi snz3.. Actually, I reposted the problem and Paolo helped me to get it working. Thanks for the reply!
A: 

The answer to this question is located here: http://stackoverflow.com/questions/899702/how-to-use-combine-2-jquery-scripts-that-use-two-different-datatypes/900851#900851

I hope it saves someone else days of pulling their hair out.