Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Wordpress Ajax script not running

Posted on 2013-05-09
8
388 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
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…

789 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