?
Solved

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

Posted on 2013-01-14
6
Medium Priority
?
270 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

743 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