I have created a contact form shortcode that I process with ajax. The shortcode have a parameter to set the email address in which the messages are going to be sent. This way I can use differents email accounts to receive the messages depending on what form is.
When you clic submmit this javascript take the form fields value with $('#name').val()
and send to the ajax_contact_form
function I created in another php file:
$('.contact-form').submit(function(){
var action = $(this).attr('action');
$("#message").slideUp(750,function() {
$('#message').hide();
$('#submit')
.after('<i class="fa fa-refresh fa-spin fa-2x"></i>')
.attr('disabled', 'disabled');
$.post(
action,
{
action: 'ajax_contact_form',
name: $('#name').val(),
email: $('#email').val(),
phone: $('#phone').val(),
subject: $('#subject').val(),
message: $('#message').val(),
},
function(data){
document.getElementById('form-alert').innerHTML = data;
$('#form-alert').slideDown('slow');
$('.contact-form .fa-spin').fadeOut('fast',function(){$(this).remove()});
$('#submit').removeAttr('disabled');
if(data.match('success') != null) $('.contact-form').slideUp('slow');
}
);
});
return false;
});
I can do the same for the email address, print in the html code like this: data-email="xxxx@xxx.com"
, but obviously I don't want to put the email address printed in the HTML code. And I don't know another way to achieve this.
How can I pass the shortcode param value to the ajax php file without printing in the form?
If you use:
$('.contact-form').serialize()
does it help? If this isn't what you wanted, could you explain a bit more?