Created
March 5, 2012 20:50
-
-
Save thomasgriffin/1980993 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
); | |
}); | |
}); |
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
this looks cool, whats it for? :P