Greg Greg - 4 months ago 20
Ajax Question

Why is my submit button hiding even when the message don't get sent?

Any idea why my submit button is hidding no matter what (message successfully sent or not sent).

When the message don't get sent I can see the #error_message appearing (which is normal) but I'd like the submit button to still be visible (to allow users to give an other try)

Thanks

$(document).ready(function() {

$('#contact_form').bootstrapValidator({
feedbackIcons: {
valid: 'fa fa-check',
invalid: 'fa fa-times',
validating: 'fa fa-refresh'
},
fields: {
first_name: {
validators: {
stringLength: {
min: 2,
},
notEmpty: {
message: 'Veuillez indiquer votre prénom'
}
}
},
last_name: {
validators: {
stringLength: {
min: 2,
},
notEmpty: {
message: 'Veuillez indiquer votre nom'
}
}
},
email: {
validators: {
notEmpty: {
message: 'Veuillez indiquer votre adresse e-mail'
},
regexp: {
regexp: '^[^@\\s]+@([^@\\s]+\\.)+[^@\\s]+$',
message: 'Veuillez indiquer une adresse e-mail valide'
}
}
},
message: {
validators: {
stringLength: {
min: 10,
max: 1000,
message:'Votre message doit faire plus de 10 caractères et moins de 1000.'
},
notEmpty: {
message: 'Veuillez indiquer votre message'
}
}
}
}}).on('success.form.bv', function (e) {
e.preventDefault();
$('button[name="submit"]').hide();

var bv = $(this).data('bootstrapValidator');
// Use Ajax to submit form data
$.post($(this).attr('action'), $(this).serialize(), function (result) {
if (result.status == 1) {
$('#success_message').slideDown({
opacity: "show"
}, "slow")
$('#contact_form').data('bootstrapValidator').resetForm();
} else {
$('#error_message').slideDown({
opacity: "show"
}, "slow") }
}, 'json');
}
);

});

Answer

You are hiding it after you press submit $('button[name="submit"]').hide();

You could show the button again when an error occures

$.post($(this).attr('action'), $(this).serialize(), function (result) {
    if (result.status == 1) {
        $('#success_message').slideDown({
            opacity: "show"
        }, "slow")
        $('#contact_form').data('bootstrapValidator').resetForm();
    } else {
        $('#error_message').slideDown({
            opacity: "show"
        }, "slow")     
        // show the button again
        $('button[name="submit"]').show();         
    }
}, 'json');