JQuery beginner: Get checkbox value THEN ajax form to 2 different scripts THEN display success

Some beginner help needed if anybody can..

I’m writing a contact form with a checkbox to signup to a MailChimp list.

I’m trying to write a JQuery script that checks if the ‘signup’ checkbox is checked then posts the form data to 2 separate PHP scripts – one which posts the data to the MailChimp list and another which sends an email with the forms contents.

How do I:

  1. Check if the checkbox is selected, then if selected:
  2. Post the data to the two scripts – but only display the success after BOTH scripts have completed

Currently the script I have works, but ignores the checkbox and displays the success message even if the first script is still processing.

Here’s what I have (edited for clarity) – I’d like to wrap this in an IF statement and then chain the ajax posts together THEN display eh success message

(Ive just thought: Am I right in saying the success part of the ajax call has no idea if the PHP part completed successfully? It just returns success if it sends the data of the the scripts ok? If I wanted to wait until the PHP scripts returned a success is it best to just put in a time delay to give them chance to run before displaying the success message?)

Many thanks for any help!

        // Add email address to mail chimp
        $  .ajax({
            type: "POST",
            url: "bin/addtomailchimp.php",
            data: 'name=' + name + '&email=' + email
        });

        // send email with form contents
        $  .ajax({
            type: "POST",
            url: "bin/enquiry_process.php",
            data: 'name=' + name + '&email=' + email + '&signup=' + signup,

            // display success message after send
            success: function() {

                // fadeOut contact form and fadein success message
                $  ('#contactform').fadeOut('slow', function() {
                        $  ('#send_success').fadeIn('slow');
                    });
                    }
        });

newest questions tagged jquery – Stack Overflow

About Admin