繁体   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