1

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;
    });
});
2
  • It’s quite hard to understand your question... Commented May 22, 2009 at 4:54
  • Are you trying to send the information? The add_customer_form data is already being sent. You are trying to send it twice.... Commented May 23, 2009 at 0:33

2 Answers 2

1

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?

Sign up to request clarification or add additional context in comments.

1 Comment

Hi snz3.. Actually, I reposted the problem and Paolo helped me to get it working. Thanks for the reply!
0
$(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;
    });
});

2 Comments

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.