Solved

Wordpress Ajax script not running

Posted on 2013-05-09
8
392 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
[X]
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
  • 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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 is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

710 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