Solved

Wordpress Ajax script not running

Posted on 2013-05-09
8
382 Views
Last Modified: 2013-05-17
Hi Experts,

I have this php/ ajax/ html script.
I can't for the life of me work out the problem.

Your fresh eyes might see the missing element:
<?php 
	$jobadder_api = new JobAdder();
	$job_cats = $jobadder_api->GetCategories();
	$job_cities = $jobadder_api->GetCities();
	$job_types = $jobadder_api->GetJobTypes();

	//ajax Insert:
function smaart_ajax_1() {
	$nonce = wp_create_nonce( 'smaart_ajax' );
	$siteurl = get_option('siteurl');
	$ajaxURL = admin_url( 'admin-ajax.php' );
	
	
	
?>
<script type='text/javascript'>
var ajaxurl = '<?php echo $ajaxURL; ?>';
var count = 0;

jQuery(function(ready){
jQuery('.searchbox').change(function()
{	

$box = this.id;
$cat = jQuery("select#job_category").val();
$loc = jQuery("select#city").val();
$type = jQuery("select#job_type").val();

//alert(ajaxurl);

//alert($box+$cat+$loc+$type);

		jQuery.ajax({
			type: "post",
			url: ajaxurl,
			data: { action: 'smaart_updateboxes',_ajax_nonce: '<?php echo $nonce; ?>', boxchanged: $box, category: $cat, location: $loc, type: $type },
			beforeSend: function() {jQuery("#loading").fadeIn('fast');}, //fadeIn loading just when link is clicked
			error: function(xhr, ajaxOptions, thrownError) {alert("Error: "+thrownError+" ResponseText: "+xhr.responseText);},
			success: function(html){ //so, if data is retrieved, store it in html
				jQuery("#loading").fadeOut('slow');
				//alert("test:" + html);
				jQuery("#formstatus").html( "test:" + html );
			}
		}); //close jQuery.ajax
}); //end 
}); //end ready

</script>
<style type='text/css'>
			#loading { clear:both; background:url(images/loading.gif) center top no-repeat; text-align:center;padding:33px 0px 0px 0px; font-size:12px;display:none; font-family:Verdana, Arial, Helvetica, sans-serif; }
			</style>
			<div id='formstatus'></div>
			<div id='loading'>LOADING!</div>
<?php
} //end smaart_ajax_1

add_action('wp_ajax_smaart_updateboxes', 'smaart_updateboxes');
add_action( 'wp_ajax_nopriv_smaart_updateboxes', 'smaart_updateboxes' );

function smaart_updateboxes() {
	//check_ajax_referer( "smaart_ajax" ); 
	echo "<h1>smaartupdateboxes ran</h1>";
	exit;
} //end
?>

Open in new window


When I run the ajax call, I simply get -1 (minus 1) instead of the "<h1>smaartupdateboxes ran</h1>" that I am expecting to come back.

Thoughts?
0
Comment
Question by:Craig Lambie
  • 5
  • 2
8 Comments
 
LVL 13

Expert Comment

by:stergium
Comment Utility
hello.
if you add a smaart_updateboxes() between 64 and 65 you still get the same results?
usually the -1 states an erroneous state of call .
please try that and feed back your results.
0
 
LVL 1

Author Comment

by:Craig Lambie
Comment Utility
Hi Stergium,

If I add that, it outputs the <h1> as expected, however not based on the ajax call.... just during the page render....
(there is a bunch of HTML below line 65 which I didn't include as not necessary I thought?
Is just the form that sets the event that runs the jquery.
Also the line
smaart_ajax_1();
to activate that function...
0
 
LVL 13

Expert Comment

by:stergium
Comment Utility
Yes you are absolutely right(i did missed that).
why does your add action statments are outside the function? do they need to be?
0
 
LVL 1

Author Comment

by:Craig Lambie
Comment Utility
yes, the add_action's are a required function to run on init, not part of the function.
Is like registering with Wordpress - http://codex.wordpress.org/AJAX_in_Plugins
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 25

Expert Comment

by:James Rodgers
Comment Utility
in firefox use firebug to determine what is actually being sent to the ajax and what is being returned

open the page , turn on firebug, refresh the page
do your submit action then on firebug click on the "Net" tab and then "All" in the sub menu
look for your post, should be last item in the list or close to it, click on it to view the details

the "post" tab will have what you submitted and the "response" tab will show you waht teh call got back, it is probably returning garbage
0
 
LVL 1

Author Comment

by:Craig Lambie
Comment Utility
Is just returning -1
See POST
Post TabAnd Response tab
response tab
Interesting observation:
Running on Firefox, I get -1, but on my laptop (first post was from desktop v of Chrome) running Chrome, I get 0.

Cheers
Craig
0
 
LVL 1

Accepted Solution

by:
Craig Lambie earned 0 total points
Comment Utility
ok, I worked it out.

Wordpress was not acting on the add_action calls I was making :(
So I added them to functions.php in the theme to ensure it was actually doing the action on loading the script.

I then had to move my function across from the file I was editing (sidebar.php) to the functions.php file so it was also accessible.

Cheers
0
 
LVL 1

Author Closing Comment

by:Craig Lambie
Comment Utility
action hooks was the problem, the return of 0 was the key - I connected it eventually.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now