Untitled

mail@pastecode.io avatar
unknown
javascript
3 years ago
3.5 kB
3
Indexable
Never
//Reserve benefits form
$('#reserveBenefitsForm').submit(function(e) {
    e.preventDefault();
    let formData = {};
    let inputNames = ['FirstName', 'LastName', 'Email', 'Mobile', 'sfmcID', 'source', 'reserveSpot', 'agent'];	
    let form = $(this);
    let failUrl = new URL(window.location.href);
    failUrl.searchParams.set('success', 'false');
    let sucessUrl = 'https://pages.sf.longandfoster.com/first-home-profile';
    if (form.valid()) {
        form.find(':input').each(function() {
            let input = $(this);
            if(inputNames.includes(input[0].name)) {
                if((input[0].type == 'radio' && input[0].checked) || input[0].type != 'radio') {
                    formData[input[0].name] = input[0].value;
                }
            }

        });

        form.html('<div class="sfmc-ajax-loader" style="text-align: center; vertical-align: middle; display: table-cell"><img src="https://image.sf.longandfoster.com/lib/fe3615707564067b721273/m/1/6eaab747-566e-4da0-a362-2458606ef744.gif"></img><p class="learn-more">&nbsp;Loading...</p></div>');

        $.ajax({
            url: 'https://pages.sf.longandfoster.com/fhfp3-ajax',
            data: JSON.stringify(formData),
            dataType: 'json',
            contentType: 'application/json',
            method: 'POST'
        }).done(function(data) {
            if (data.status == 'success') {
                gtag( 'event', 'Submit:Form-Success', { 'event_category': 'Web Form',  'event_label' : 'First Home Campaign'});
                window.location.href = sucessUrl + '&leadId=' + data.leadId;
            }
            else {
                gtag( 'event', 'Submit:Form-Fail', { 'event_category': 'Web Form',  'event_label' : 'First Home Campaign'});
                window.location.href = failUrl.href;
            }
            
        }).fail(function() {
            gtag( 'event', 'Submit:Form-Fail', { 'event_category': 'Web Form',  'event_label' : 'First Home Campaign'});
            window.location.href = failUrl.href;
        });
    }
});


//email form
$('#unqueId').submit(function(e) {
    e.preventDefault();
    let formData = {};
    let inputNames = ['Email'];	
    let form = $(this);
    if (form.valid()) {
        form.find(':input').each(function() {
            let input = $(this);
            if(inputNames.includes(input[0].name)) {
                if((input[0].type == 'radio' && input[0].checked) || input[0].type != 'radio') {
                    formData[input[0].name] = input[0].value;
                }
            }

        });

        form.html('<div class="sfmc-ajax-loader" style="text-align: center; vertical-align: middle; display: table-cell"><img src="https://image.sf.longandfoster.com/lib/fe3615707564067b721273/m/1/6eaab747-566e-4da0-a362-2458606ef744.gif"></img><p class="learn-more">&nbsp;Loading...</p></div>');
        $.ajax({
            url: 'https://pages.sf.longandfoster.com/fhfp4-ajax',
            data: JSON.stringify(formData),
            dataType: 'json',
            contentType: 'application/json',
            method: 'POST'
        }).done(function(data) {
            if (data.status != 99) {
                console.log('SUCCESS!!!!');
            }
            else {
                console.log('FAIL!!!!');
            }
            
        }).fail(function() {
            console.log('FAIL!!!!');
        });
    }
});