Skip to content

Instantly share code, notes, and snippets.

@thomasgriffin
Created March 5, 2012 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasgriffin/1980993 to your computer and use it in GitHub Desktop.
Save thomasgriffin/1980993 to your computer and use it in GitHub Desktop.
jQuery(document).ready(function($) {
$('.wl_add_item, .wl_remove_item').submit(function(e) {
e.preventDefault();
/** Determine if we are adding or removing items and build variables from there */
var current_object = $(this); // Get the current object - we will use it when appending stuff
var action_type = $(this).attr('class');
if ( 'wl_add_item' == action_type ) {
var process_this = 'add';
var quantity = $(this).find('input[name="wl_add_quantity"]').val();
var post_id = $(this).find('input[name="wl_add_post_id"]').val();
} else {
var process_this = 'remove';
var quantity = $(this).find('input[name="wl_remove_quantity"]').val();
var post_id = $(this).find('input[name="wl_remove_post_id"]').val();
}
console.log(process_this);
console.log(quantity);
console.log(post_id);
/** Prepare our data to be sent via Ajax */
var data = {
action: 'myajax-submit',
type: process_this,
wl_quantity: quantity,
wl_post_id: post_id
};
/** Process the Ajax response and log the response */
$.post(
MyAjax.ajaxurl,
data,
function(response) {
/** Remove any old appended data */
$('#example-output').remove();
/** Create our new appended data */
var output = '';
output += '<div id="example-output"><p>So this is the ID that we just sent:' + response['post_id'] + '.</p></div>';
/** I'm fading into add, but you can determine where to fade by sending back data that determines what action happened */
$(current_object).fadeIn('normal', function() { // Use the submit object as the reference point for appending
$(this).append(output);
});
},
'json'
);
});
});
@jaredatch
Copy link

this looks cool, whats it for? :P

@thomasgriffin
Copy link
Author

I was helping out a guy in the #wordpress IRC channel with Ajax and this is a sample I came up with for him. :-P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment