admin管理员组文章数量:1430083
I have a link in worpress page
<a onclick="show_trend()" >Trend</a>
and i am calling an ajax function onclick.
function show_trend() {
// This does the ajax request
$.ajax({
url: ajaxurl,
data: {
'action':'render_admin_charts_page',
},
success:function(data) {
// This outputs the result of the ajax request
console.log(data);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
}
what i want to do when calling this function is display a div section .for that ,this is my function
function render_admin_charts_page() {
?>
<div class="wrap">
<div class="em-bookings-events">
<h2><?php esc_html_e('Event Booking Report','dbem'); ?></h2>
<?php em_bookings_events_table(); ?>
</div>
</div>
<?php
}
ajax function is calling but not rendering to div
I have a link in worpress page
<a onclick="show_trend()" >Trend</a>
and i am calling an ajax function onclick.
function show_trend() {
// This does the ajax request
$.ajax({
url: ajaxurl,
data: {
'action':'render_admin_charts_page',
},
success:function(data) {
// This outputs the result of the ajax request
console.log(data);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
}
what i want to do when calling this function is display a div section .for that ,this is my function
function render_admin_charts_page() {
?>
<div class="wrap">
<div class="em-bookings-events">
<h2><?php esc_html_e('Event Booking Report','dbem'); ?></h2>
<?php em_bookings_events_table(); ?>
</div>
</div>
<?php
}
ajax function is calling but not rendering to div
Share Improve this question asked Jul 3, 2014 at 5:50 Nisham MahsinNisham Mahsin 1292 silver badges10 bronze badges3 Answers
Reset to default 1To add action for Wordpress AJAX
If you needed to create an AJAX handler for an "render_admin_charts_page" request, you would create a hook like this:
add_action( 'wp_ajax_render_admin_charts_page', 'yourfunction' );
function yourfunction() { ?>
<div class="wrap">
<div class="em-bookings-events">
<h2><?php esc_html_e('Event Booking Report','dbem'); ?></h2>
<?php em_bookings_events_table(); ?>
</div>
</div>
<?php // Handle request then generate response using WP_Ajax_Response
}
Using the above example, any time an AJAX request is sent to WordPress, and the request's 'action' property is set to 'render_admin_charts_page', this hook will be automatically executed. For example, the following code would execute the above hook:
jQuery.post(
ajaxurl,
{
'action': 'render_admin_charts_page',
},
function(response){
alert('The server responded: ' + response);
}
);
Basically you want to declare this function, add_action( 'wp_ajax_render_admin_charts_page', 'yourfunction' );
, in another file?
If yes, then you can place this piece of code in functions.php of your active theme:
add_action( 'wp_ajax_render_admin_charts_page', 'yourfunction' );
function yourfunction() { ?>
<div class="wrap">
<div class="em-bookings-events">
<h2><?php esc_html_e('Event Booking Report','dbem'); ?></h2>
<?php em_bookings_events_table(); ?>
</div>
</div>
<?php // Handle request then generate response using WP_Ajax_Response
}
If you want to place it in another PHP file, for that you don't need to create a hook for AJAX. Just place your code in a PHP file and place it in your active theme, and use the below code for AJAX:
jQuery.ajax({
url: fav_url , // path of that file
type: 'GET',
timeout: 20000,
error: function(){
alert("Error loading user's attending event.");
},
success: function(html){}
})
Hey I am doing same thing but instead of using wp_ajax_render I am using wp ajax nopriv function and it is working for me.
本文标签: jqueryCreating a new div onclick wordpressajax
版权声明:本文标题:jquery - Creating a new div onclick wordpress+ajax 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745555896a2663182.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论