dshgnt2008 2018-04-04 15:05
浏览 71
已采纳

将AJAX数据作为JSON保存到WP表

I've setup a function and some jQuery that takes a value from a form a user submits, and sends it to the database successfully in it's own table. However, I'm trying to set it up to save the data as JSON instead (so as to make sure my WP table doesn't balloon to an unbearable size in the future).

Function here:

function search_modifications_callback() {

// Ensure we have the data we need to continue
if( ! isset( $_POST ) || empty( $_POST ) || ! is_user_logged_in() ) {

    header( 'HTTP/1.1 400 Empty POST Values' );
    echo 'Could Not Verify POST Values.';
    exit;
}

$user_id = get_current_user_id();  // Get our current user ID
$search_term  = $_POST['saved_search'];

update_user_meta( $user_id, 'saved_search', $search_term ); // Add our user meta


wp_update_user( array(
    'ID'            => $user_id
) );

exit;
}
add_action( 'wp_ajax_nopriv_search_ss', 'search_modifications_callback' );
add_action( 'wp_ajax_search_ss', 'search_modifications_callback' );

jQuery here:

// Form submission listener
jQuery( '#save-search' ).click( function() {

    // Grab our post meta value
    var ss_val = jQuery( '#save_search_term' ).val();
    var user_id = jQuery( '#user_id' ).val();

    // Do very simple value validation
    if( jQuery( '#save_search_term' ).val() ) {
        jQuery.ajax( {
            url : ajax_url,                 
            type: 'POST',                   
            data: {                         
                action  : 'search_ss',          
                id: user_id,
                'saved_search': ss_val,       
            }
        } )
        .success( function( results ) {
            console.log( 'User Meta Updated!' );
            alert('Search saved!');
        } )
        .fail( function( data ) {
            console.log( data.responseText );
            console.log( 'Request failed: ' + data.statusText );
        } );

    } else {

    }

    return false;   // Stop our form from submitting
} );

I understand that instead of using 'update_user_meta' I should probably create an object instead, but am unsure how to properly set that up from here.

Open to other suggestions as well. Thanks!

  • 写回答

1条回答 默认 最新

  • 普通网友 2018-04-04 16:22
    关注

    Taking your comment above into consideration, if you plan to save up to N search terms per user then I'd do something like this:

    function search_modifications_callback() {
    
        // Ensure we have the data we need to continue
        if( ! isset( $_POST ) || empty( $_POST ) || ! is_user_logged_in() ) {
            header( 'HTTP/1.1 400 Empty POST Values' );
            echo 'Could Not Verify POST Values.';
            exit;
        }
    
        $user_id = get_current_user_id();  // Get our current user ID
        $search_term  = $_POST['saved_search'];
    
        // Get user's saved search terms
        $user_saved_search_terms = get_user_meta( $user_id, 'saved_search' );
    
        // User has some previously saved search terms
        if (
            $user_saved_search_terms
            && is_array( $user_saved_search_terms )
            && !empty( $user_saved_search_terms )
        ) {
    
            // This search term doesn't exist in our saved search terms array yet, so let's add it
            if ( !in_array($search_term, $user_saved_search_terms) ) {
                $user_saved_search_terms[] = $search_term;
    
                /**
                 * You'll want to add here some logic
                 * to limit the array to N search terms
                 *
                 * For example:
                 */
    
                /*
                // User has more than 5 saved search terms already,
                // let's remove the oldest one
                if ( count($user_saved_search_terms) > 5 ){
                    $user_saved_search_terms = array_shift( $user_saved_search_terms );
                }
                */
            }
        } // User does not have any search terms saved yet
        else {
            $user_saved_search_terms = array( $search_term );
        }
    
        // Finally, save the data
        update_user_meta( $user_id, 'saved_search', $user_saved_search_terms );
    
        // What is this for?
        wp_update_user( array(
            'ID'            => $user_id
        ) );
    
        exit;
    }
    add_action( 'wp_ajax_nopriv_search_ss', 'search_modifications_callback' );
    add_action( 'wp_ajax_search_ss', 'search_modifications_callback' );
    

    This way, you still will have only one saved_search record per user in your database.

    Please note that this code hasn't been tested, so if you have any questions / comments let me know.

    By the way, what if the user isn't logged-in? Are you checking for this scenario somewhere?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染