【问题标题】:Call PHP function using ajax in WordPress在 WordPress 中使用 ajax 调用 PHP 函数
【发布时间】:2020-01-05 19:07:44
【问题描述】:

我想使用 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 有人请帮助我做错了什么?

【问题讨论】:

  • @RobinZigmond 您好,先生,我已经用我的 ajax 更新了我的问题。请检查
  • 谢谢。不幸的是,除了 Ajax 调用出现问题之外,警报并没有告诉我们太多任何信息。您能否查看开发人员工具的网络部分并查看 Ajax 请求发生了什么:特别是 URL、HTTP 状态和响应(如果有)。

标签: javascript php ajax wordpress


【解决方案1】:

将此代码粘贴到您的 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';
    } 
}

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-04
    • 2015-07-10
    • 1970-01-01
    • 2012-08-19
    • 2011-05-08
    • 2011-01-17
    相关资源
    最近更新 更多