Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Wordpress Ajax script not running

Posted on 2013-05-09
8
Medium Priority
?
403 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
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
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

Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

Question has a verified solution.

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

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 …
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 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 reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…

719 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