开发者

jQuery/Ajax form success message

开发者 https://www.devze.com 2023-04-10 01:28 出处:网络
I have a couple of forms on a site I\'m working on and the script that controls them doesn\'t include a success message, so when they\'re submitted the input data just disappears and the user doesn\'t

I have a couple of forms on a site I'm working on and the script that controls them doesn't include a success message, so when they're submitted the input data just disappears and the user doesn't know if it's been actually sent or not. I've looked around a bit for answers, but because this file controls an email submission form, a contact form, and a twitter feed, it's a bit much fo开发者_StackOverflow中文版r me to see what's what.

Here's the code, I'd just like to let users know that their message has been sent for both the email input form and the contact form. I appreciate any help that's out there!

$(document).ready(function() {
//Set default hint if nothing is entered
setHints();

//Bind JavaScript event on SignUp Button
$('#signUp').click(function(){
    signUp($('#subscribe').val());

});

//Bind JavaScript event on Send Message Button
$('#sendMessage').click(function(){    
    if(validateInput()){
        sendMail();
    }else
    {
        alert('Please fill all fields to send us message.');
    }
});

//Load initial site state (countdown, twitts)
initialize();
});
var setHints = function()
{
$('#subscribe').attachHint('Enter your email to be notified when more info is available');
$('[name=contact_name]').attachHint('Name');
$('[name=contact_email]').attachHint('Email');
$('[name=contact_subject]').attachHint('Subject');
$('[name=contact_message]').attachHint('Message');
};
var signUp = function(inputEmail)
{
var isValid = true;
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if(!emailReg.test(inputEmail)){
    isValid = false;
    alert('Your email is not in valid format');
}
if(isValid){
    var params = {
        'action'    : 'SingUp',
        'email'     : inputEmail
    };
    $.ajax({
        type: "POST",
        url: "php/mainHandler.php",
        data: params,
        success: function(response){
            if(response){
                var responseObj = jQuery.parseJSON(response);
                if(responseObj.ResponseData)
                {
                    $('#subscribe').val('');
                }
            }
        }
    });
}
};
var initialize = function()
{
var params = {
    'action'    : 'Initialize'
};
$.ajax({
    type: "POST",
    url: "php/mainHandler.php",
    data: params,
    success: function(response){
        if(response){
            var responseObj = jQuery.parseJSON(response);
            if(responseObj.ResponseData)
            {
                $('ul.twitts').empty();
                if(responseObj.ResponseData.Twitts){
                    $('a.followUsURL').attr('href','http://twitter.com/#!/'+responseObj.ResponseData.Twitts[0].Name);
                    $.each(responseObj.ResponseData.Twitts, function(index, twitt){
                        var twitterTemplate = '<li>'
                        + '<a href="http://twitter.com/#!/{0}/status/{1}" target="_blank" class="twittURL">#{0}</a>'
                        + '{2}'
                        + '<span class="time">{3}</span>'
                        + '</li>';

                        $('ul.twitts').append(StringFormat(twitterTemplate, twitt.Name, twitt.StatusID, twitt.Text, twitt.Date));
                    });
                }

                if(responseObj.ResponseData.Start_Date)
                {
                    setInterval(function(){
                        var countDownObj = calculateTimeDifference(responseObj.ResponseData.Start_Date);
                        if(countDownObj){
                            $('#days').text(countDownObj.Days);
                            $('#hours').text(countDownObj.Hours);
                            $('#minutes').text(countDownObj.Minutes);
                            $('#seconds').text(countDownObj.Seconds);
                        }
                    }, 1000);
                }
            }
        }
    }
});
};

var validateInput = function(){
var isValid = true;
$('input, textarea').each(function(){
    if($(this).hasClass('required'))
    {
        if($(this).val()!=''){
            if($(this).hasClass('email'))
            {
                var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                if(!emailReg.test($(this).val())){
                    isValid = false;
                    alert('Your email is not in valid format');
                }
            }
        }else
        {
            isValid = false;
        }
    }
});
return isValid;
};

var resetInput = function(){
$('input, textarea').each(function() {
    $(this).val('').text('');
});
};

var sendMail = function(){
var params = {
    'action'    : 'SendMessage',
    'name'      : $('[name=contact_name]').val(),
    'email'     : $('[name=contact_email]').val(),
    'subject'   : $('[name=contact_subject]').val(),
    'message'   : $('[name=contact_message]').val()
};
$.ajax({
    type: "POST",
    url: "php/mainHandler.php",
    data: params,
    success: function(response){
        if(response){
            var responseObj = jQuery.parseJSON(response);
            if(responseObj.ResponseData)
                $('label.sendingStatus').text(responseObj.ResponseData);
        }
        resetInput();
        $('#sendMail').removeAttr('disabled');
    },
    error: function (xhr, ajaxOptions, thrownError){
        //xhr.status : 404, 303, 501...
        var error = null;
        switch(xhr.status)
        {
            case "301":
                error = "Redirection Error!";
                break;
            case "307":
                error = "Error, temporary server redirection!";
                break;
            case "400":
                error = "Bad request!";
                break;
            case "404":
                error = "Page not found!";
                break;
            case "500":
                error = "Server is currently unavailable!";
                break;
            default:
                error ="Unespected error, please try again later.";
        }
        if(error){
            $('label.sendingStatus').text(error);
        }
    }
});
};

var calculateTimeDifference = function(startDate) {
var second = 1000;
var minute = second * 60;
var hour = minute * 60;
var day = hour * 24;

var seconds = 0;
var minutes = 0;
var hours = 0;
var days = 0;

var currentDate = new Date();
startDate = new Date(startDate);

var timeCounter = startDate - currentDate;
if (isNaN(timeCounter))
{
    return NaN;
}
else
{
    days = Math.floor(timeCounter / day);
    timeCounter = timeCounter % day;

    hours = Math.floor(timeCounter / hour);
    timeCounter = timeCounter % hour;

    minutes = Math.floor(timeCounter / minute);
    timeCounter = timeCounter % minute;

    seconds = Math.floor(timeCounter / second);
}

var tDiffObj = {
    "Days" : days,
    "Hours" : hours,
    "Minutes" : minutes,
    "Seconds" : seconds
};

return tDiffObj;
};

var StringFormat = function() {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i++) {
    var regExpression = new RegExp("\\{" + i + "\\}", "gm");
    s = s.replace(regExpression, arguments[i + 1]);
}
return s;
}


You need to hook into the success callbacks of each of the $.ajax calls. You can create a method that will show a message for those:

For example, your signUp function's success callback could look like:

success: function(response){
    if(response){
        var responseObj = jQuery.parseJSON(response);
        if(responseObj.ResponseData)
        {
            $('#subscribe').val('');
            showMessage('Your subscription was received. Thank you!');
        }
    }
}

And you just create a method that will show the message to the user

var showMessage = function (msg) {
    // of course, you wouldn't use alert, 
    // but would inject the message into the dom somewhere
    alert(msg);
}

You would call showMessage anywhere the success callback was fired.


You can add your success notifing code in each of the $.ajax success handlers.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号