so, have been struggling making ajax work. here previous question: ajax (admin_url('admin-ajax.php');?action=) not found
anyway, decided narrow down , have necessary files.
here set up.
test.php
<div class="test"> <a href="#test_demo" id="demo">items</a> </div> <div id="test_demo"> </div> <script> jquery(document).ready(function() { jquery('.test a').click(function(e) { e.preventdefault(); var tab_id = jquery('this').attr('id'); jquery.ajax({ type: "get", url: "<?php echo admin_url('admin-ajax.php'); ?>", datatype: 'html', data: ({ action: 'test_tab', id: tab_id}), success: function(data){ jquery('#test_' + tab_id).html(data); }, error: function(data) { alert("error!"); return false; } }); }); }); </script> function.php
function test_tab_callback() { $template_part_path = 'page-parts/test_' . $_get['id']; get_template_part($template_part_path); exit; } add_action('wp_ajax_test_tab', 'test_tab_callback'); add_action('wp_ajax_nopriv_test_tab', 'test_tab_callback'); test_demo.php
<div id="test_demo_content">demo content</div> here idea on how should work.
- test.php: when user clicks
itemsbutton,tab_idvariable in jquery saves anchorid(in case,id="demo"). - then admin-ajax.php called.
- the saved id ("demo") passed onto function.php , used in variable
$template_part_path = 'page-parts/test_' . $_get['id'];givespage-parts/test_demotest_demo.php then template part called , calledback jquery.
then data "insert"
jquery('#test_' + tab_id).html(data);id="test_demo.- the test_demo.php content should displayed in
#test_demodiv.
but not working. used console.log(data) showed no result. doing wrong?
when getting jquery object of "this" can't use quotes. making tab_id undefined , ruining rest.
here mean:
http://jsfiddle.net/7r1dg7l4/2/
jquery(document).ready(function() { jquery('.test a').click(function(e) { e.preventdefault(); var tab_id = jquery(this).attr('id'); alert(tab_id); }); });
Comments
Post a Comment