1

I have a page showing all transactions made via paypal on my site. I want to add a button that could delete that specific row.

The transaction id is unique.

how would I be able to when user clicks on the button to delete just that row? also to run a jquery alert to confirm delete?

my code:

<table class"widefat">
<?php  global $wpdb;
    $result = $wpdb->get_results ( "SELECT * FROM wp_donations" );
    if (!$result){?>
    <tr>
    <td>There are no donations to show</td>
    </tr>
<?php }else{?> 
<thead>
<tr>
 <th>Name</th>
 <th>Email</th>
 <th>Phone Number</th>
 <th>Address</th>
 <th>Amount</th>
 <th>Method</th>
 <th>Date</th>
 <th>Transaction ID</th>
 <th></th>
</tr>
</thead>
  <?php foreach ( $result as $print )   {?>
    <tr>
    <td><?php echo $print->name;?></td>
    <td><?php echo $print->email;?></td>
    <td><?php $phones = $print->phone; if($phones == 0){echo 'No Number Provided';}else{echo $phones;}?></td>
    <td><?php echo $print->address;?></td>
    <td>$<?php echo $print->amount;?></td>
    <td><?php echo $print->method;?></td>
    <td><?php echo $print->dates;?></td>
    <td><?php echo $print->txid;?></td>
    <td><input type="button" id="<?php echo $print->txid;?>" class="delete" title="Delete" value="delete" /></td>
    </tr>
        <?php }?>
       </table>

2 Answers 2

2

If jquery is an option , then it is very simple :

$(".delete").on('click',function(){
$(this).parents("tr").remove();
})

check this

Sign up to request clarification or add additional context in comments.

6 Comments

appreciate it - trying to remove the row from the database
yeh - i know I need to run a function once the button is clicked - wpdb->delete just not sure how to do
very simple , check this : stackoverflow.com/questions/5967322/…
@Mark you will need to use ajax to pass the corresponding row , what makes every row unique ?
the transaction id. the question you posted doesn't really help me.
|
1

I ended up running an action. using the unique ID that is AUTO_INCREMENT in the database.

and then called the action on page load.

$actions = array(

            'delete' => sprintf('<a href="?page=%s&action=delete&id=%s">%s</a>', $_REQUEST['page'], $item['id'], __('Delete', 'custom_table_example')),
        );

   function process_bulk_action()
    {
        global $wpdb;
        $table_name = $wpdb->prefix . 'donations'; // do not forget about tables prefix

        if ('delete' === $this->current_action()) {
            $ids = isset($_REQUEST['id']) ? $_REQUEST['id'] : array();
            if (is_array($ids)) $ids = implode(',', $ids);

            if (!empty($ids)) {
                $wpdb->query("DELETE FROM $table_name WHERE id IN($ids)");
            }
        }
    }

1 Comment

Hi Mark, I cant understand your code. can you please explain it? I am not able to understand how delete works in wpdb

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.