• I’m trying to change some entries in the wpdb, to change the status of reservations in a booking calendar to approved on a successful payment. It doesn’t seem to work, and I really have no clue as to why. I’m pretty new to wordpress, php and javascript, so it’s probably something really silly.

    Here’s my code:

    Functions.php:

    function set_db_transient() {
         if(isset($_COOKIE['user_key'])){
            set_transient($_COOKIE['user_key'] . 'database_id', $_POST['database_id'], 600); 
         }
         wp_die();
    }
    
    add_action( 'wp_ajax_set_db_transient', 'set_db_transient' );
    add_action( 'wp_ajax_nopriv_set_db_transient', 'set_db_transient' );
    
    function filter_woocommerce_payment_successful_result() {     
        global $wpdb;
        $change_status = $wpdb->update($wpdb->prefix . 'reservations_table_name', array('status' => "approved"), array('id' => get_transient($_COOKIE['user_key'] . 'database_id')));    
    }; 
             
    add_filter( 'woocommerce_payment_complete', 'filter_woocommerce_payment_successful_result');

    JS ajax call:

    database_id = $("input[name='reservationid']").val();			
    jQuery.post( 'wp-admin/admin-ajax.php', { 'action': 'set_db_transient', 'database_id': database_id });
    • This topic was modified 3 years, 9 months ago by klaivu.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Don’t use relative URLs in WP related code. There will be situations where what is relative will be wrong. Use a full, absolute URL to post Ajax requests. You can use admin_url() to construct a full URL.

    Get your basic code working first, but you’ll eventually need to validate and sanitize any data coming in from the browser. Never add unsanitized data to your DB, it’s a major security risk. To further secure the exchange, include a nonce value in the served page, which is then sent along with the Ajax request for your Ajax handler to verify. Otherwise a bad actor could submit malicious Ajax data from anywhere.

    Use your browser’s network developer tool to verify the request is properly formed. Verify data within your Ajax handler is as expected by sending debug data to the error log with error_log().

Viewing 1 replies (of 1 total)
  • The topic ‘Problems with $wpdb’ is closed to new replies.