Solved

My jquery .load returns HTML in Chrome, but all other browsers it returns "0"

Posted on 2013-01-14
6
266 Views
Last Modified: 2013-01-20
This is the javascript (relevant line) - I'm using wordpress, hence the call to the admin page with an action parameter...
$j('#city_statistics-div').load('wp-admin/admin-ajax.php', { action: 'city_statistics', t: '-1' });

Open in new window


this is the php
/*
 * City & Timezone Form
 */
function ttw_city_statistics() {
	$city_id = $_REQUEST[POST_CITY_URL];
	if( $city_id ) {
		echo ttw_city_statistics_html( $city_id );
	}
	die();
}


function ttw_city_statistics_html( $city ) {
	if( -1 == $city) {
		$select  = 'Latitude <input type="text" size="15" name="latitude" id="latitude" value="" />';
		$select .= 'Longitude <input type="text" size="15" name="longitude" id="longitude" value="" />';
		$select .= 'TimeZone ' . ttw_timezone_dropdown_html( '+00:00' );
		$select .= '<input type="checkbox" name="daylight" id="daylight" />Observes Daylight Savings';
	} elseif( 0 < $city ) {
		$result = ttw_get_city_sp( $city ) ;
		$select  = 'Latitude <input type="text" size="15" name="latitude" id="latitude" value="' . $result->Latitude . '" />';
		$select .= 'Longitude <input type="text" size="15" name="longitude" id="longitude" value="' . $result->Longitude . '" />';
		$select .= 'TimeZone ' . ttw_timezone_dropdown_html( $result->TimeZone );
		$select .= '<input type="checkbox" name="daylight" id="daylight" ' . (($result->daylight) ? 'checked=checked' : '') . ' />Observes Daylight Savings';
	}
		
	return $select;
}

Open in new window


In Chrome it works great - I get the input boxes I'm looking for.  In all other browsers, instead of the html, a "0" is printed in the div....

What silly thing have I forgotten????!
0
Comment
Question by:PaultheBroker
  • 4
  • 2
6 Comments
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 38778025
Not sure how your code works in any browser. You're loading a php file which contains 2 functions and there is no explicit call to either of them. If you just call that script normally, you will get no output, because the actual functions are never called.
0
 
LVL 6

Author Comment

by:PaultheBroker
ID: 38778482
Sure.  The function is being called.  As  i said ... its Wordpress.  I omitted this as being irrelevant -

add_action( 'wp_ajax_city_statistics' , 'ttw_city_statistics' );

Not to give you a Wordpress tutorial, but the call to the admin-ajax.php page with the action=city_statistics triggers the wordpress 'action' "wp_ajax_city_statistics" which then causes ttw_city_statistics() to be called.  That's just the way WordPress likes to roll - it ensures the WordPress framework is loaded when you perform the ajax call.
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 38778753
Ah. OK, that makes sense. Not sure I can really help on this one - sorry!
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 6

Author Comment

by:PaultheBroker
ID: 38779647
Update - I thought that the ajax call was working but now that I test it on another computer, it seems nothing is working...so this might be a general ajax thing.
0
 
LVL 6

Accepted Solution

by:
PaultheBroker earned 0 total points
ID: 38781605
OK - thanks for your help.  I'm officially a dolt....the ajax call I was making required me to be logged in to work...which is why it worked in Chrome (because I had logged in there) but not anywhere else.  

add_action( 'wp_ajax_nopriv_city_statistics' , 'ttw_city_statistics' );

is the way to call ajax from WordPress if you aren't logged in :S
0
 
LVL 6

Author Closing Comment

by:PaultheBroker
ID: 38798132
I figured this out finally....after a lot of wasted time!  I hope it helps someone else - or I might just delete it out of shame!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

776 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