Wordpress Php函数通过ajax传递变量来保存用户元

时间:2015-02-04 00:42:18

标签: php ajax wordpress variables

自上次发布以来,所有代码都已更新。 我在编写这段代码时遇到了麻烦。这是给客户的。它应该做的是当用户以客户身份登录网站时。他们填写产品标准:年份,品牌,型号,选项1.这将生成我的客户所拥有的产品列表。他想要一个按钮来保存客户的标准,这样他们就可以在用户部分点击它来提取产品而无需再次进行搜索。

我遇到麻烦并传递标准。所以在我的js文件中,这就是我对ajax函数的作用:

function ajaxAddVehicleMeta(){

var userId = ''; // fill this in to somehow acquire the userID client-side...
var year = '<?php echo $year; ?>';
var make= '<?php echo $make; ?>';
var model= '<?php echo $model; ?>';
var option1= '<?php echo $option1; ?>';
var stdoropt= '<?php echo $stdoropt; ?>'; 

jQuery.ajax({
   type: 'POST',
        data: { 
            action: 'saveVeh', 
            userId: userId, 
            year:year, 
            make:make, 
            model:model, 
            option1:option1, 
            stdoropt:stdoropt },
        url: comm_custom.ajaxurl, //this is the url found in functions
        success: function(msg){
     alert( "Data Saved: User ID " + userId + "." );
   },
   error: function(msg){
      alert( "Data not saved: User ID " + userId + ".");
   }

});
}

在我的functions.php文件中,我有:

add_action( 'wp_ajax_saveVeh', 'saveVeh' );
add_action( 'wp_ajax_nopriv_saveVeh', 'saveVeh' ); 
function saveVeh(){

$year = $_POST['year'];
$make = $_POST['make'];
$model = $_POST['model'];
$option1 = $_POST['option1'];
$stdoropt = $_POST['stdoropt'];

    //if ( is_user_logged_in() ) {
        $user_id = get_current_user_id();

        if(!$user_id):
                echo 'failed, not logged in';
                exit;
            endif;  

        if($year){
            $vehicle1 = $year.' '.$make.' '.$model.' '.$option1.' / '.$stdoropt;
        }else if($_POST['width']){
            $vehicle1 = $_POST['width']. ' ' .$_POST['height']. ' ' .$_POST['rim'];
        }
            update_user_meta( $user_id, '_vehicle1', $vehicle1);            
    //}

页面上有以下链接:已更新

<a href="#" onclick="ajaxAddVehicleMeta()" class="vehicle-btn" >Save Search Criteria</a>

我发出警报,它告诉我它成功了。但它并未将其保存在用户元数据中。有什么我做错了吗?

comm_custom.ajaxurl是enqueue脚本中的admin-ajax.php并对其进行本地化。

1 个答案:

答案 0 :(得分:0)

上面的代码中有一些问题..使用前需要设置变量,否则会抛出错误。

JS

        function ajaxAddVehicleMeta(){

        var tireyear= jQuery('#tireyear').val(); // select id of input change to whatever it actually is
        var width= jQuery('#width').val(); // select id of input change to whatever it actually is

        jQuery.ajax({
           type: 'POST',
                data: { 
                    action: 'saveVeh', 
                    userId: userId, 
                    tireyear: tireyear,
                    width: width            
                    },
                url: comm_custom.ajaxurl, //this is the url found in functions
                success: function(msg){
             alert( "Data Saved: User ID " + userId + "." );
           },
           error: function(msg){
              alert( "Data not saved: User ID " + userId + ".");
           }

        });
    }

PHP

        add_action( 'wp_ajax_saveVeh', 'saveVeh' );
    add_action( 'wp_ajax_nopriv_saveVeh', 'saveVeh' ); 

    function saveVeh(){


            $user_id = get_current_user_id();

            if(!$user_id):
                echo 'failed, not logged in';
                exit;
            endif;  

            if($_POST['tire-year']){
                $vehicle1 = $_POST['tire-year'].' '.$_POST['make'].' '.$_POST['model'].' '.$_POST['option1'].' / '.$_POST['stdoropt'];
            }else if($_POST['width']){
                $vehicle1 = $_POST['width']. ' ' .$_POST['height']. ' ' .$_POST['rim'];
            } else {
                echo 'failed, no data';
                exit;
            }

            update_user_meta( $user_id, '_vehicle1', $vehicle1);            

            echo $user_id; // you need to return something for ajax success to pick up..
            exit;
    }

html: - 你的函数中没有参数传递值。所以留空吧..

<a href="#" onclick="ajaxAddVehicleMeta()" class="vehicle-btn" >Save Vehicle 1</a>