jquery - Accessing $_POST data in functions.php from a template file after it's been sent via ajax -
i able retrieve data sent via ajax in functions.php. need data in template.php (of theme). first time using ajax , maybe i'm going wrong way. able echo $_post['myvar']; within functions.php (i posting code once work). assuming setup correct, can access ajax data outside of functions.php? btw, signed here @ stack well, if failed follow procedure, apologize.
edited
thanks guys - here sample code. in js file have:
$(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 have:
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 $parent_cat value. use $parent_cat outside of functions.php i.e in template.php. far, i've tried creating function global variable:
function set_global_var($new_value) { global $my_global_var; $my_global_var = $new_value; }
and calling inside ajax function:
set_global_var($parent_cat);
to further explain goal, i'm trying selected value drop-down. value parent category name generated wp_dropdown_categories. after getting it, parent category's sub categories. thought of ajax (which i've never used before) because i'd have 2 drop-down options - 1 parent categories, , other subcategories generated selected parent category(possibly without refreshing page). thanks.
follow step rendor data in template file. there 2 way rendor data can implement same time denpands on situation.
- you sending ajax request admin panel , want display on frontend
- you sending ajax request form front end , want display on frontend
if sending ajax request admin panel , want update template(front end) in case first have stroe data in database. said accessed data through functions.php file have update_option() add or update data wordpress database. add have check , fetch in template get_option() rendor on page refresh front end.
now situation two, sending data front end , want update also. in case follow first step update_option()
, get_option()
, send response functions.php
want display in front page(only on same page used send request.) , and display it. if refresh page same updated data rendor on template.
hope understand tried explain.
Comments
Post a Comment