【问题标题】:Wordpress Php function passing variables through ajax to save user metaWordpress Php 函数通过 ajax 传递变量以保存用户元数据
【发布时间】:2015-02-04 00:42:18
【问题描述】:

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

我在使用 ajax 和通过标准时遇到问题。所以在我的 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 并对其进行本地化。

【问题讨论】:

  • data: { action: 'saveVeh', userId: userId } 您没有在此处发布任何您正在获取的内容(php 中的 $_POST)。你也有 add_action 到 ajax_nopriv 的功能吗?
  • 我添加了 ajax_nopriv 操作。我必须在数据部分更改什么?
  • 现在你只传递了 $_POST['userId'],所以你在 php 中的 post 值为 null。但这不是您的主要问题, is_logged in 必须返回 false 否则您将保存一个空的数据库条目。删除它进行测试。
  • 我尝试添加 $_POST['tire-year'] 之类的,但我收到类似“userId:userId,tire-year:tire-year ...”的错误说它们是未定义的...我想在 onclick 的实际链接中添加更多内容吗?
  • 你需要在传入之前设置变量。如果你只是想测试使用轮胎年:'轮胎年'等......实际上使用轮胎年代替

标签: php ajax wordpress variables


【解决方案1】:

您在上面的代码中存在一些问题..js 变量需要在使用之前设置,否则它们会引发错误。

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>

【讨论】:

  • 此链接所在的页面是结果页面。该链接用于保存结果的选项:年份、品牌、型号、选项 1... 这会改变 var 结构吗?
  • 是的......这都是简单的js,暂时忽略ajax标签。在函数中设置要发送到服务器的任何变量。 jQuery(selector).val() 将从您的信息所在的 html 字段中提取值。如果它们已经由 php 设置,您可以将它们回显到您的 js 中,例如var 变量= 。然后像上面一样创建数据字段(参见 tyreyear 示例),然后这显然可以在服务器上使用 $_POST['tyreyear']。
  • 看起来没问题,它工作正常吗?在服务器端处理 $_POST 变量时,您还需要执行 sanitize_text_field()。
  • 它工作正常。我在哪里放置sanitize_text_field?在 javascript 代码中还是在页面本身上?
  • 它是一个 wordpress php 函数。例如$var= sanitize_text_field($_POST['var']); ,在你的 saveVeh 函数中使用它
猜你喜欢
  • 2012-04-20
  • 2022-09-23
  • 1970-01-01
  • 1970-01-01
  • 2021-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-08
相关资源
最近更新 更多