• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

Problem with Wordpress and AJAX

Hi.

I have a Wordpress site and I want to refresh some data when user click on a link, but without reloading the full page, so I'm trying to use AJAX in Wordpress. I have added this code to functions.php file:

do_action('wp_ajax_nopriv_poll_results','show_poll_results');
do_action('wp_ajax_poll_results','show_poll_results');

function show_poll_results() {
   echo 'Hey!';
}

Open in new window


And also I have this snipped of code on my page:

	<div id="sidebar-poll-results" class="widget mainbox">
<?php show_poll_results(); ?>
	</div><!-- end #side-leaders -->

Open in new window


and finally I have this jQuery code on the same page:
	        jQuery('#poll_view_results', this).click(function() {
			show_results();
			return false;
	        });

Open in new window


and finally I have this code that mades the ajax call:

	function show_results() {
		$.post("/wp-admin/admin-ajax.php", {'action':'poll_results'}, function(response) {
			$('#sidebar-poll-results').html(response);
		});
	}

Open in new window


When page is loaded, contents appear well (it appears the "Hey!" message, so php show_poll_results functions is working, but, after clicking on the link #poll_view_results, response variable of the ajax callback functions is getting a "-1" instead of "Hey!". Does anybody knows why this could happen ?

Thanks
0
gplana
Asked:
gplana
  • 4
  • 4
1 Solution
 
Julian HansenCommented:
What do you get if you browse to
/wp-admin/admin-ajax.php?action=poll_results?
0
 
gplanaAuthor Commented:
Hi julianH. Thanks for your answer.

I'm getting "-1" (without quotes), so just 2 characters, looking at source html code, so I'm not getting html but this 2 characters.

So it looks like show_poll_results function is not beign executed. Maybe the hook addition I made on functions.php is not ok ?
0
 
Julian HansenCommented:
It would seem that way - your show_poll_results is being executed at page render time so the function is working - but your call back to the function is not - the URL rules out a GET / POST issue and also indicates that the action is not being wired correctly.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
gplanaAuthor Commented:
Thanks. I'm not sure if I fully understand. Do you mean the problem is on do_action() or on ajax call ?

and what can be the solution ?

Thanks.
0
 
Julian HansenCommented:
The problem is not the AJAX call.

Here is what we know

1. When you load the page - hey is displayed - so the show_poll_results function is successfully called when the page is first generated.

2. When you access the link directly (the link used by the AJAX call) you get a -1 (or -2) result

3. You get the same result when you call from AJAX - which is obvious.

From what you have posted there is nothing wrong with your linking the click to the AJAX call or the AJAX call itself - the problem is therefore in how the event in Word Press for the action is being wired to the show_poll_results - you need to look there to solve the problem

The key thing is to get this URL to work when typed into the browser address bar
/wp-admin/admin-ajax.php?action=poll_results?

Once that works - then the AJAX call should work .
0
 
gplanaAuthor Commented:
Ok, thanks. But I read do_action is the right way to link my function to a wordpress event, and wp_ajax_nopriv_xxx and wp_ajax_xxx are the events to use for ajax call with action param = xxxxx; so I don't understand what's wrong here.

You give me the clue of the problem, but can you or anyone else explain me what is wrong with these wordpress hooks ?

Thanks.
0
 
Julian HansenCommented:
Difficult without seeing more code - for instance - have you put your add_action calls into your code (http://codex.wordpress.org/Function_Reference/add_action)?
0
 
gplanaAuthor Commented:
Thank you very much. You solved my problem. I should use add_action instead of do_action. This was the problem. Now I 'm getting some other errors, but the function is beign executed.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now