[wp-hackers] Delete Row - Refreshes Page but not DB

J.D. Grimes jdg at codesymphony.co
Wed Sep 25 19:50:15 UTC 2013


It isn't hard to do, I've done it before. Do you know jQuery? jQuery's AJAX handlers are very easy to user (in my opinion, at least). Quick and probably somewhat dirty example:

jQuery( document ).ready( function( $ ) {
	
	// When a button is clicked within the table with id="mytable"
	$( '#mytable button' ).click( function( event ) {
		
		// Keep the default action from happening (form submitted/page refreshed)
		event.preventDefault();

		if ( ! confirm( 'Are you sure you want to continue' ) )
			return;

		// Get the id of the row to delete.
		// I'd recommend just making the button value the row ID, no need for the extra input.
		// Then you can access it easier through $( this ).val();
		var rowID = $( this ).parent().child( 'input' )[0].val();

		// POST data.
		var data = {
			'action': 'my_delete_row_action',
			'rowID': rowID
		}

		// Send AJAX request to delete this.
		$.post( 
			ajaxurl, 
			data, 
			function ( response ) {
				
				// Check if this was successful.
				// Assumes you are using http://codex.wordpress.org/Function_Reference/wp_send_json_success
				if ( ! response.success ) {

					alert( 'Failure!' );
				}
					
				// Success! Hide the row.
				$( '#mytable tr#myrow-' + rowID ).hide();
			}
		);
	});
});

OK, that probably won't work off the bat, just an example. You'll need to give your table an HTML ID, and give each row a unique ID based on $i (in the example it's myrow-$i.

Also you'll need to see http://codex.wordpress.org/AJAX_in_Plugins for info on how to handle the AJAX request properly within WordPress.

I'm sure you can find some great tutorials out there to help you along, too.

J.D.

On Sep 25, 2013, at 3:22 PM, BenderisGreat <greglancaster71 at gmail.com> wrote:

> Right you are [again] J.D.
> 
> So should I just start sending you paypal payments for your assistance?  
> One more followup question -  I don't know any Ajax- is there a simple way
> (now that the form is fixed) to remove that row without a page refresh?  
> 
> Again, not familiar with ajax so coding that myself isn't an option yet, but
> maybe there is a simple way to implement something like that?
> 
> 
> 
> --
> View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Delete-Row-Refreshes-Page-but-not-DB-tp42393p42403.html
> Sent from the Wordpress Hackers mailing list archive at Nabble.com.
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list