Sweet Alert 2,WP Ajax

时间:2017-10-03 06:18:03

标签: php jquery ajax wordpress action

Necesito usar Sweet alert 2 para borrar registros de una BD la consola dice que esta mal esta parte:

data: { "action": "doDEL", 'delete='+productId }

显示以下错误: 未捕获的SyntaxError:意外的标记+

按钮

echo'<tr><a href="javascript:void(0)"><span class="dashicons dashicons-trash del-button" id="delete_product" data-id="'.$row["persona_id"].'" title="Borrar"></span></a></td></tr>';

JS

<script>
 $(document).ready(function(){
//  readProducts(); /* it will load products when document loads */
    var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
  $(document).on('click', '#delete_product', function(e){
   var productId = $(this).data('id');
   SwalDelete(productId);
   e.preventDefault();
  });  
 });
 function SwalDelete(productId){
  swal({
   title: 'Seguro...?',
   text: "texto",
   type: 'warning',
   showCancelButton: true,
   confirmButtonColor: '#3085d6',
   cancelButtonColor: '#d33',
   confirmButtonText: 'Aceptar',
   showLoaderOnConfirm: true,
   preConfirm: function() {
     return new Promise(function(resolve) {
        jQuery.ajax({
        url: ajaxurl,
        type: 'POST',
        dataType: 'json',
        data: { "action": "doDEL", 'delete='+productId }
        })
        .done(function(response){
         swal('Deleted!', response.message, response.status);
         // hacer algo en respuestas
        })
        .fail(function(){
         swal('Oops...', 'Something went wrong with ajax !', 'error');
        });
     });
      },
   allowOutsideClick: false     
  }); 
 }
 </script>

PHP wp action

$table = 'table';
add_action( 'wp_ajax_ejemplo', 'doDEL' );

function doDEL() {
    global $wpdb;

    header('Content-type: application/json; charset=UTF-8');
    $response = array();
    if ($_POST['delete']) {
        $pid = intval($_POST['delete']);
        $query = "DELETE FROM {$table} WHERE product_id=:pid";
        $stmt = $DBcon->prepare( $query );
        $stmt->execute(array(':pid'=>$pid));   
        if ($stmt) {
            $response['status']  = 'success';
            $response['message'] = 'Product Deleted Successfully ...';
        } else {
            $response['status']  = 'error';
            $response['message'] = 'Unable to delete product ...';
        }
        echo json_encode($response);
    }
}

我不太了解ajax请帮忙

0 个答案:

没有答案