AJAX issues (Cannot get ajax to successfully work) PHP, AJAX, JAVASCRIPT, WORDPRESS (NO PLUGIN)

前端 未结 1 902
有刺的猬
有刺的猬 2021-01-28 12:51

I asked a question earlier today with little luck: AJAX within a foreach loop

i was put on the right path but the answer provided has got me more confused than ever. Whe

相关标签:
1条回答
  • 2021-01-28 13:06

    there have been 2 problems in your PHP script:
    1. the function was not called
    2. the return-value of the function was not echoed.

    (in order to respond to an AJAX call, the called url always needs to output something - JSON in the most cases. AJAX can't access PHPand thus can't read PHP return values.)

    here is the fixed version:

    function update_this_func($remove_option, $uservideo_id){
    
        global $wpdb;
    
        $wpdb->query("UPDATE " . $wpdb->prefix."uservideo
                      SET is_removed =" . $remove_option . "
                      WHERE uservideo_id =" . $uservideo_id );
    
        return json_encode(['status' => 'Updated!']); // return status as json
    }
    echo update_this_func($_POST['remove_option'], $_POST['uservideo_id']);
    
    0 讨论(0)
提交回复
热议问题