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

BenderisGreat greglancaster71 at gmail.com
Sat Oct 19 07:21:19 UTC 2013


*I was having a hard time working out how to implement the ajax into
wordpress with the JSON callback.  So, I hired a guy from freelancer.  His
job was to setup my form as ajax, but im looking at his code now and it does
not make use the same code you used.  

What he did was echo the row-ID in each 
 (is this safe?) and then created this button:*

<div id="delete<?php echo $row['id']; ?>">
<input type="submit" name="deleteItem"
value="delete" class="delete-box"
onclick="delete1(<?php echo $row['id']; ?>) ">delete
</div>

*and this <div>, which shows inside the 
 I click delete on:*

<div style="display:none"  id="areusure<?php echo
$row['id']; ?>"></div>

*Then, in the head of the file this is the script that handles the selection
of the 
 etc...*




*and finally, here is the ajax call (which isnt written how you suggested
using ajaxurl in wordpress)*


<?php
    if ( !isset($wp_did_header) ) {
    $wp_did_header = true;
    require_once('../../../wp-load.php' );
    wp();
    require_once( ABSPATH . WPINC . '/template-loader.php' );

}
global $wpdb;
if (isset($_REQUEST['id'])) {
	$id = $_REQUEST['id'];
	$file = $wpdb->get_var("select file from wp_jo_plugin_options WHERE id =
$id");
	//wp_delete_attachment( $file );
	$qry2 = $wpdb->prepare("CREATE TABLE IF NOT EXISTS t2  AS SELECT * from 
wp_jo_plugin_options WHERE id = %s", $id);
	$jo1 = $wpdb->query($qry2);
	$qry = $wpdb->prepare("DELETE FROM wp_jo_plugin_options WHERE id = %s",
$id);
	$jo_remove_row = $wpdb->query($qry);
	}
	if (isset($_REQUEST['undo'])) {
	$undo = $_REQUEST['undo'];
	$qry2 = $wpdb->prepare("insert ignore into wp_jo_plugin_options SELECT *
from  t2 WHERE id = %s", $undo);
	$jo_remove_row = $wpdb->query($qry2);
	}
?>


*Now - I have no idea is this is bad code, but it does not match the same
layout you suggested.  Is this safe? It cost me about 100 bucks on
freelancer.com.  :-/*



J.D. Grimes wrote
> 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@

> > 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 .automattic

>> http://lists.automattic.com/mailman/listinfo/wp-hackers
> 
> _______________________________________________
> wp-hackers mailing list

> wp-hackers at .automattic

> http://lists.automattic.com/mailman/listinfo/wp-hackers


J.D. Grimes wrote
> 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@

> > 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 .automattic

>> http://lists.automattic.com/mailman/listinfo/wp-hackers
> 
> _______________________________________________
> wp-hackers mailing list

> wp-hackers at .automattic

> http://lists.automattic.com/mailman/listinfo/wp-hackers





--
View this message in context: http://wordpress-hackers.1065353.n5.nabble.com/Delete-Row-Refreshes-Page-but-not-DB-tp42393p42560.html
Sent from the Wordpress Hackers mailing list archive at Nabble.com.


More information about the wp-hackers mailing list