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

Gregory Lancaster greglancaster71 at gmail.com
Sat Oct 19 07:25:27 UTC 2013


<b>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 < tr  > (is this safe?) and then
created this button:</b>

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

<b>and this <div>, which shows inside the <tr> I click delete on:</b>

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

<b>Then, in the head of the file this is the script that handles the
selection of the < tr > etc...</b>

< script type="text/javascript">
var iii=0;
function delete1(id)
{
$.ajax(
{
url: '../../wp-content/plugins/gogonow/ajax1.php?id='+id,
success:function(data){
iii = setTimeout(function(){rm(id)},8000);
$("#areusure"+id).html($("#tr"+id).html());
$("#tr"+id).css("background-color","#FF3700");
$("#tr"+id).html("<td colspan='4'>Are you sure you want to delete it...<a
href='javascript:undo1("+id+")'>Undo</a ></ td >");
}
});
}
function undo1(id) {
$.ajax( {
url: '../../wp-content/plugins/gogonow/ajax1.php?undo='+id,
success:function(data){
clearTimeout(iii);
$("#tr"+id).html($("#areusure"+id).html());
}
})
}
function rm(id) {
$('#tr'+id).fadeOut(400);
}
<  /script>


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


<?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);
}
?>


<b>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.  :-/</b>


On Wed, Sep 25, 2013 at 12:50 PM, J.D. Grimes <jdg at codesymphony.co> 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 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
>
> _______________________________________________
> 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