/* Plugin para enviar forms a destinos php via ajax hecho por Andrés 12/03/2014
- Nombre: sendform
- Dependencias:
agregar antes del fin del div container del bootstrap:
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
- Opciones del plugin necesarias en el index
type: POST o GET
url: ruta a donde enviar el form
reenvio: ruta a donde redirigir luego del envio exitoso
*/
(function ( $ ) {
$.fn.sendform = function( options ) {
// This is the easiest way to have default options.
var settings = $.extend({
// These are the defaults.
type: 'POST',
url: '',
reenvio: 'index.php'
}, options );
return this.each(function() {
$.validator.setDefaults({
highlight: function(element) {
$(element).closest('.form-group').addClass('has-error');
},
unhighlight: function(element) {
$(element).closest('.form-group').removeClass('has-error');
},
errorElement: 'span',
errorClass: 'help-block',
errorPlacement: function(error, element) {
if(element.parent('.input-group').length) {
error.insertAfter(element.parent());
} else {
error.insertAfter(element);
}
}
});
var esto = $(this);
esto.validate({
rules: {
captcha_code: {
required: true,
remote: {
url: 'validc.php',
type: 'post'
}
}
},
messages: {
captcha_code: {
remote: 'Código inválido'
}
},
/*,*/
submitHandler: function(form) {
var datos = esto.serialize();
$.ajax({
type: settings.type,
url: settings.url,
data: datos,
success:function(data){
$('#myModal .modal-body').html(data);
$('#myModal').modal({show:true});
$('#myModal').on('hide.bs.modal', function (e) {
window.location.href = settings.reenvio;
});
}
});
}
});
});
};
}( jQuery ));