How to combine two JQuery functions

Can someone please give me a hand and tell me what is wrong with this script? The answer that was posted doesn't work. What I get when I use this code is the entire rendered html page. This is what I see on the console. Can someone please help me? 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;
    });
});

      

+1


a source to share


2 answers


Change possible

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

      

to



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

      

would do the trick?

+1


a source


$(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;
    });
});

      



0


a source







All Articles