简体   繁体   English

通过ajax发送后,从模板文件访问functions.php中的$ _POST数据

[英]Accessing $_POST data in functions.php from a template file after it's been sent via ajax

I am able to retrieve data sent via ajax in functions.php. 我能够在functions.php中检索通过ajax发送的数据。 But I need that data in template.php (of my theme). 但是我需要(我的主题)template.php中的数据。 This is my first time using ajax and maybe I'm going about it the wrong way. 这是我第一次使用Ajax,也许我正在以错误的方式进行操作。 I am able to echo $_POST['myvar']; 我能够回显$ _POST ['myvar']; within the functions.php (I will be posting the code once back to work). 在functions.php中(一旦恢复工作,我将发布代码)。 Assuming the setup is correct, can I access the ajax data outside of the functions.php? 假设设置正确,是否可以在functions.php之外访问ajax数据? Btw, I just signed up here at Stack as well, so if failed to follow some procedure, I apologize. 顺便说一句,我也是在Stack上注册的,所以如果未能遵循某些程序,我深表歉意。

Edited 已编辑

Thanks guys - here is a sample code. 谢谢大家-这是示例代码。 In the js file I have: 在js文件中,我有:

$(window).load(function(){
      $("#cat").on("click",function() {


         var selectedCat = $(this).children("option").filter(":selected").text();

          $.get('../../../../../../wp-admin/admin-ajax.php', {

                action:  "parent_cat_send",

                parent_cat: selectedCat

             });

          });
    });

And in functions.php I have: 在functions.php中,我有:

    add_action('wp_ajax_parent_cat_send', 'current_par_cat');
   add_action('wp_ajax_nopriv_parent_cat_send', 'current_par_cat');

   function current_par_cat() {
       global $parent_cat;

      $parent_cat = $_GET['parent_cat'];

      echo $parent_cat;

            wp_die();

   }

The response has the $parent_cat value. 响应具有$ parent_cat值。 I would like to use $parent_cat outside of functions.php ie in the template.php. 我想在functions.php之外使用$ parent_cat,即在template.php中。 So far, I've tried creating a function with global variable: 到目前为止,我已经尝试创建具有全局变量的函数:

function set_global_var($new_value)
{
    global $my_global_var;
    $my_global_var = $new_value;
}

and calling it inside the ajax function: 并在ajax函数中调用它:

 set_global_var($parent_cat);

To further explain my goal, I'm trying to get the selected value from a drop-down. 为了进一步说明我的目标,我正在尝试从下拉列表中获取选定的值。 This value is the parent category name generated by wp_dropdown_categories. 该值是wp_dropdown_categories生成的父类别名称。 After getting it, I would like to then get that parent category's sub categories. 获得它之后,我想获得该父类别的子类别。 I thought of Ajax (which I've never used before) because I'd like to have two drop-down options - one with the parent categories, and the other with subcategories generated by the selected parent category(possibly without refreshing the page). 我想到了Ajax(我以前从未使用过),因为我想拥有两个下拉选项-一个带有父类别,另一个带有由选定父类别生成的子类别(可能不刷新页面) 。 Thanks. 谢谢。

Follow the step to rendor your data in you template file. 请按照以下步骤在模板文件中保存数据。 There are two way to rendor data that you can implement same time but its denpands on the situation. 您可以同时实现两种提供数据的方式,但是这取决于情况。

  1. You are sending ajax request from admin panel and want to display on frontend 您正在从管理面板发送ajax请求,并希望在前端显示
  2. You are sending ajax request form front end and want to display on frontend 您正在发送ajax请求表单的前端,并希望在前端显示

If you are sending ajax request from admin panel and want to update the template(front end) in this case first you have to stroe the your data in database. 如果您要从管理面板发送ajax请求,并且想在这种情况下首先更新模板(前端),则必须在数据库中查找数据。 As you said you accessed the data through functions.php file now you have to update_option() to add or update your data to wordpress database. 就像您说的那样,您现在已经通过functions.php文件访问了数据,您必须要update_option()才能将数据添加或更新到wordpress数据库中。 Add have to check and fetch it in your template with get_option() But it will only rendor on page refresh from front end. 添加必须使用get_option()检查并在模板中获取它,但它只会从前端刷新页面。

Now situation two, You are sending data from front end and want to update it also. 现在情况二,您正在从前端发送数据,并且还想对其进行更新。 in this case follow the first step to update_option() and get_option() and also send response from functions.php that you want to display in your front page(Only on the same page that you used to send request.) and and display it. 在这种情况下,请先执行update_option()get_option()第一步, update_option()从要显示在首页(仅在用于发送请求的同一页面上)的functions.php中发送响应并显示它。 So that if you refresh page then the same updated data will rendor on your template. 这样,如果刷新页面,则相同的更新数据将保存在模板上。

Hope you understand what i tried to explain. 希望您理解我试图解释的内容。

maksbd19 gave me the right answer as I can't access the variable outside of functions.php. maksbd19给了我正确的答案,因为我无法在functions.php之外访问变量。 Not sure how to mark the response as the correct answer but here it is: 不确定如何将响应标记为正确答案,但是这里是:

"agreed with @rjdown it is impossible to assist you with this little information. But what I can say for now is as far as I know you can not use your ajax code in your theme file/s as they are not loaded when your ajax request is parsed. Only the functions.php file is loaded and you should have all of your business logic in this file only. – maksbd19 Apr 26 at 3:28" “与@rjdown达成协议,无法为您提供这些小信息。但是,我现在只能说,我知道您无法在主题文件中使用ajax代码,因为当您的ajax加载时它们不会被加载请求已解析。仅加载functions.php文件,并且您应仅在此文件中包含所有业务逻辑。– maksbd19 Apr 26,3:28“

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM