Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Wordpress Ajax script not running

Posted on 2013-05-09
8
Medium Priority
?
406 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
ID: 39154619
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
ID: 39154624
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
ID: 39154641
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:Craig Lambie
ID: 39154737
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
 
LVL 25

Expert Comment

by:James Rodgers
ID: 39157385
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
ID: 39157621
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
ID: 39160398
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
ID: 39174166
action hooks was the problem, the return of 0 was the key - I connected it eventually.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
The purpose of this video is to demonstrate how to properly insert a Vimeo Video into a WordPress site or Blog. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…

926 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