0

I would like to delete row from front end. This is what i did :

HTML :

<?php $row = get_row_index(); ?>
<tr data-rownum="'. $row .'">
  <td>
    <span class="deletecontrat iconfont_d" data-toggle="tooltip" data-placement="right" title="Effacer ?">r</span>
  </td>
</tr>

AJAX.JS

jQuery( document ).on( 'click', '.deletecontrat', function() {

    $rownumjs = $(this).parent().parent().data("rownum");

    jQuery.post(             
            ajaxurl,
            {
                'action': 'mon_action',
                'param': $rownumjs
            }
            ,
            function(response){
                    console.log(response);
            }
    );

});

FUNCTIONS.PHP

function add_js_scripts() {
    wp_enqueue_script( 'script', get_template_directory_uri().'/custom/js/ajax.js', array('jquery'), '1.0', true );
    wp_localize_script('script', 'ajaxurl', admin_url( 'admin-ajax.php' ) );
}
add_action('wp_enqueue_scripts', 'add_js_scripts');




function mon_action() {

    $param = $_POST['param'];

    delete_row("field_5c8fa4201b65f", 1, $post_id);

    echo $param;
    die();
}
add_action( 'wp_ajax_mon_action', 'mon_action' );
add_action( 'wp_ajax_nopriv_mon_action', 'mon_action' );

So in my console i get response :

  • 1 for the first row
  • 2 for the second row

and so on…

I assume that my code working, but now i can't or i don't know how to execute my delete function… I have read this, but nothing happen.

What am i doing wrong ?

(I hope i'm cristal clear because english is not my language.)

1

You have 1 hardcoded as the row number in mon_action().

It should be delete_row( 'field_5c8fa4201b65f', (int) $_POST['param'], $post_id );

4
  • Hum yes but it was for a test and i've tried with delete_row( 'field_5c8fa4201b65f', $param, $post_id ); It doesn't work either
    – Fluti
    Apr 22 '19 at 18:58
  • How are you getting $post_id? Apr 22 '19 at 19:02
  • But just with function mon_action() { echo "<script>console.log('the message function is called');</script>"; die(); } i have no response in ma console log
    – Fluti
    Apr 23 '19 at 11:50
  • That wouldn't work as you're expecting it to. The rest of your code works fine. Apr 23 '19 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.