簡體   English   中英

在WordPress中使用ajax調用PHP函數

[英]Call PHP function using ajax in WordPress

我想使用ajax調用php函數。 這是我的表單代碼

    <form class="woocommerce-form woocommerce-form-login login" method="post">
    <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
        <label for="reg_billing_name"><?php esc_html_e( 'Full Name', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label>
        <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="reg_billing_name" id="reg_billing_name" autocomplete="reg_billing_name" value="<?php echo ( ! empty( $_POST['reg_billing_name'] ) ) ? esc_attr( wp_unslash( $_POST['reg_billing_name'] ) ) : ''; ?>" />
    </p>

    <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
        <label for="reg_billing_email"><?php esc_html_e( 'Email Address', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label>
        <input type="email" class="woocommerce-Input woocommerce-Input--text input-text" name="reg_billing_email" id="reg_billing_email" autocomplete="reg_billing_email" value="<?php echo ( ! empty( $_POST['reg_billing_email'] ) ) ? esc_attr( wp_unslash( $_POST['reg_billing_email'] ) ) : ''; ?>" />
    </p> 

    <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
        <label for="reg_billing_phone"><?php esc_html_e( 'Mobile Number', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label>
        <input type="tel" class="woocommerce-Input woocommerce-Input--text input-text" name="reg_billing_phone" id="reg_billing_phone" autocomplete="reg_billing_phone" value="<?php echo ( ! empty( $_POST['reg_billing_phone'] ) ) ? esc_attr( wp_unslash( $_POST['reg_billing_phone'] ) ) : ''; ?>" />
    </p> 
    <button type="submit" class="woocommerce-Button button" onclick="customerregister();"><?php esc_html_e( 'Register', 'woocommerce' ); ?></button>
    </form>

<script>
 function customerregister() {
            $.ajax({
            url: "<?php echo get_home_url(); ?>/wp-admin/admin-ajax.php",
            type: 'POST',
            dataType: 'JSON',
            data: {
                action: 'devsol_customer_auth_register'
            },
            success: function(data, status, xhr){
                console.log(data)
                if(data.data == 'success')
                    window.location = 'http://localhost/final/my-account'
                else
                    alert("Error: No account found with this Mobile number. Please register now and get 10% Discount coupon.")

            },
            error: function (xhr, ajaxOptions, thrownError) {
                // this error case means that the ajax call, itself, failed, e.g., a syntax error
                // in your_function()
                alert ('Request failed: ' + thrownError.message) ;
            },
        })
     }       
</script>

我想調用這個php函數

function devsol_customer_auth_register() {
    $customer_name = sanitize_text_field( $_POST['reg_billing_name'] );
    $customer_phone = sanitize_text_field( $_POST['reg_billing_phone'] );
    $customer_email = sanitize_email( $_POST['reg_billing_email'] );    
    $customer_password = wp_generate_password( 12, true );       

if ( !username_exists($customer_phone) && !email_exists($customer_email) ) {       
        $user_id = wp_create_user($customer_phone, $customer_password, $customer_email);                                 
        if ( !is_wp_error( $user_id ) ) {   

        wp_clear_auth_cookie();
        wp_set_current_user ( $user_id );
        wp_set_auth_cookie  ( $user_id );

        wp_update_user(array( 
            'ID' => $user_id,
            'first_name' => $customer_name,
            'role' => 'customer', ));      
        }      
    }   
    else {
        echo 'Account already existed';
    }  
}

如何使用 ajax 或 javascript 在 WordPress 中調用 php 函數。

我在警報框中收到一條錯誤消息,錯誤是:請求失敗:未定義

我在我的問題中添加了 php、html 和 javascript ajax 有人請幫助我我做錯了什么嗎?

將此代碼粘貼到您的 function.php 中

add_action( 'wp_ajax_devsol_customer_auth_register', 'devsol_customer_auth_register' );
add_action( 'wp_ajax_nopriv_devsol_customer_auth_register', 'devsol_customer_auth_register' );


function devsol_customer_auth_register() {

    $customer_name = sanitize_text_field( $_REQUEST['reg_billing_name'] );
    $customer_phone = sanitize_text_field( $_REQUEST['reg_billing_phone'] );
    $customer_email = sanitize_email( $_REQUEST['reg_billing_email'] );    
    $customer_password = wp_generate_password( 12, true );       

    if ( !username_exists($customer_phone) && !email_exists($customer_email) ) {       
            $user_id = wp_create_user($customer_phone, $customer_password, $customer_email);                                 
            if ( !is_wp_error( $user_id ) ) {   

            wp_clear_auth_cookie();
            wp_set_current_user ( $user_id );
            wp_set_auth_cookie  ( $user_id );

            wp_update_user(array( 
                'ID' => $user_id,
                'first_name' => $customer_name,
                'role' => 'customer', ));      
            } 
            $user = new WP_User( $user_id );
            $user->set_role( 'customer' );
            wp_mail( $customer_email, 'New User Registration', 'Your Password: ' . $customer_password );     
        }   
    else {
        echo 'Account already existed';
    } 
}

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM