Solved

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

Posted on 2013-01-14
6
267 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 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