Jump to content

Wordpress PHP & Ajax Auto Submit Help


asifmohammed1978

Recommended Posts

Hi,

 

I'm a novice and have the below code in a Wordpress theme. I'm trying to get the form to submit automatically so it logs an entry into the MySql db as though the person clicked the heart icon (submit). Ideally I subsequently want to hide the heart icon so on load maybe after 3 seconds the entry is auto fired to the MySql DB.

 

I have got as far as the manual click adding the entry, however can't get to to auto submit, any gel would be appreciated. Thank you in advance.

 

////PHP Form/////

<form class="ajax-form" action="<?php echo AJAX_URL; ?>" method="POST">
<?php if(ThemexUser::islastviewed(ThemexUser::$data['active_user']['ID'])) { ?>
  <a href="#" title="<?php _e('Remove from lastviewed', 'lovestory'); ?>" data-title="<?php _e('Add to Last Viewed', 'lovestory'); ?>" class="icon-heart submit-button current"></a>
  <input type="hidden" class="toggle" name="user_action" value="remove_lastviewed" data-value="add_lastviewed" />
<?php } else { ?>
  <a href="#" title="<?php _e('Add to Last Viewed', 'lovestory'); ?>" data-title="<?php _e('Remove from Last Viewed', 'lovestory'); ?>" class="icon-heart submit-button"></a>
  <input type="hidden" class="toggle" name="user_action" value="add_lastviewed" data-value="remove_lastviewed" />
<?php } ?>
<input type="hidden" name="user_lastviewed" value="<?php echo ThemexUser::$data['active_user']['ID']; ?>" />
<input type="hidden" class="nonce" value="<?php echo wp_create_nonce(THEMEX_PREFIX.'nonce'); ?>" />
<input type="hidden" class="action" value="<?php echo THEMEX_PREFIX; ?>update_user" />

</form> 

 

////Ajax/////

//Elements

var themeElements = {
ajaxForm: '.ajax-form',
}
 
//AJAX Form
$(themeElements.ajaxForm).each(function() {
var form=$(this);
 
form.submit(function() {
var message=form.find('.message'),
loader=form.find('.loader'),
toggle=form.find('.toggle'),
button=form.find(themeElements.submitButton),
title=form.find(themeElements.submitButton).data('title');
 
var data={
action: form.find('.action').val(),
nonce: form.find('.nonce').val(),
data: form.serialize()
}
 
loader.show();
button.addClass('disabled').toggleClass('current');
 
if(!message.hasClass('static')) {
message.slideUp(300, function() {
$(themeElements.colorboxLink).colorbox.resize();
});
}
 
jQuery.post(form.attr('action'), data, function(response) {
if(jQuery('.redirect', response).length) {
if(jQuery('.redirect', response).attr('href')) {
window.location.href=jQuery('.redirect',response).attr('href');
} else {
window.location.reload();
}
 
message.remove();
}
 
if(title) {
button.data('title', button.attr('title'));
button.attr('title', title);
}
 
toggle.each(function() {
var value=toggle.val();
toggle.val(toggle.data('value'));
toggle.data('value', value);
});
 
loader.hide();
button.removeClass('disabled');
 
if(response!='' &&  response!='0' && response!='-1') {
if(message.hasClass('popup')) {
$.colorbox({html:'<div class="popup">'+response+'</div>'});
} else if(message.hasClass('static')) {
message.append(response);
} else {
message.html(response).slideDown(300, function() {
$(themeElements.colorboxLink).colorbox.resize();
});
}
}
 
form.find('.temporary').val('');
form.find('.scroll').each(function() {
$(this).scrollTop($(this)[0].scrollHeight);
});
});
 
return false;
});
});
 
Thank you
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.