mardi 26 janvier 2016

javascript combine two with IF

I am trying to combine two javascripts to one using IF.

I want IF script1 is True to run only script1. And IF script1 is False to run script2.

Script1= Check all field with "required" if empty in a contact form. Script2= Send email using ajax.

Script1

  $('document').ready(function () {
    $('form#contact-form').submit(function(e) {

        var ref = $(this).find("[required]");

        $(ref).each(function(){
            if ( $(this).val() == '' )
            {
                alert("Required field should not be blank.");

                $(this).focus();

                e.preventDefault();
                return false;
            }
        });  return true;
    });
});

Script2

   $('document').ready(function () {
    $('form#contact-form').submit(function () {
        var form = $(this);
        var post_data = form.serialize(); //Serialized the form data for process.php
        $('#loader').html('<img src="../spinner.gif" /> Please Wait...');

        form.fadeOut(500, function () {
            form.html("<h3>Thank you.").fadeIn();
            $('#loader').html('');
        });

        // Normally would use this
        $.ajax({
            type: 'POST',
            url: 'process.php', // Your form script
            data: post_data,
            success: function(msg) {
                form.fadeOut(500, function(){
                    form.html(msg).fadeIn();
                });
            }
        });

        return false;
    });
});

Aucun commentaire:

Enregistrer un commentaire