提交而不重新加载页面

时间:2019-06-23 14:51:42

标签: php

有一个Wordpress插件。这是MyCred。我有一个固定的代码:

/**
 * Shortcode: Take Points
 * This custom shortcode allows you to take points from your users when they
 * click on the button this shortcode generates. Supports optional confirmation message.
 * @version 1.0.2
 */
add_shortcode( 'mycred_take', 'mycred_pro_render_take_shortcode' );
function mycred_pro_render_take_shortcode( $atts, $label = 'Give Away' ) {

    extract( shortcode_atts( array(
        'user_id' => '',
        'confirm' => '',
        'amount'  => '',
        'unique'  => 0,
        'ref'     => 'mycred_take',
        'entry'   => '%plural% lose',
        'done'    => 'You have lost points',
        'ctype'   => 'mycred_default'
    ), $atts ) );

    if ( ! is_user_logged_in() || ! function_exists( 'mycred' ) ) return '';

    if ( $user_id == '' )
        $user_id = get_current_user_id();

    // Load essentials
    $user_id = absint( $user_id );
    $mycred = mycred( $ctype );

    // User is excluded = has no balance
    if ( $mycred->exclude_user( $user_id ) ) return '';

    // Unique check
    if ( $unique == 1 && $mycred->has_entry( $ref, 0, $user_id, '', $ctype ) ) return '';

    $balance = $mycred->get_users_balance( $user_id, $ctype );

    $output = '';

    // If button was pushed
    if ( isset( $_POST['mycred-take-points-token'] ) && wp_verify_nonce( $_POST['mycred-take-points-token'], 'mycred-deduct-points' . $ref . $ctype ) ) {

        // Deduct
        $mycred->add_creds(
            $ref,
            $user_id,
            0 - $amount,
            $entry
        );

        // Update balance
        $balance = $balance - $amount;

        if ( $done != '' )
            $output .= '<p>' . $done . '</p>';

    }

    // Too low balance
    if ( $balance < $amount ) return '';

    $onclick = '';
    if ( $confirm != '' )
        $onclick = '
<script type="text/javascript">
( function( $ ) {
    $( "form#mycred-take-shortcode' . $ref . $ctype . '" ).on( "submit", function(){
        if ( ! confirm( \'' . $confirm . '\' ) ) return false;
    });
} )( jQuery );
</script>';

    return $output . '<form action="" method="post" id="mycred-take-shortcode' . $ref . $ctype . '"><input type="hidden" name="mycred-take-points-token" value="' . wp_create_nonce( 'mycred-deduct-points' . $ref . $ctype ) . '" /><input type="submit" class="button" value="' . $label . '" /></form>' . $onclick;

}

此简单代码基于设置来更新用户的余额。但是然后刷新页面。我的目标不是刷新页面,只有用户刷新页面。

我尝试了onclick="return false"-但这不起作用。 另外,我尝试将输入更改为按钮,但无法正常工作,但输入类为按钮。

0 个答案:

没有答案