This topic is locked

Use of jQuery and ajax

7/14/2011 12:48:48 PM
PHPRunner General questions
B
bluedude author

Hi,
I want to do something like this on a page:
function showplaces(category) {

$.ajax({
type: "GET",

url: "api_fs.php",

//data: "category=" + category,

success: function(html){

$('#response').html(html);

}

});

}
The php file loads stuff from a database and puts it in the response div. For some reason I cannot get it to work with jQuery.
Do I need to add a jQuery script tag or is it automatically loaded?
The Showplaces is called on a marker drag in a Google Map.

C
cgphp 7/14/2011

Please, post the php code of the file that loads stuff from a database.
Before to make the ajax request, be sure that the php file works correctly.

B
bluedude author 7/15/2011

Hi,
I'm 100% sure the PHP code works because i tested it from a normal HTML page. The jQuery gives a problem because it says in my browser it is not defined.
jQuery is a pain in PHPrunner.
include '../includes/ApiClient.php';
//read GET fields

$reqfields = array("location");

//browse throught the fields

foreach ($reqfields as $field) {

${$field} = addslashes($_GET[$field]);

}
try {

$latitude="52.368918";

$longitude="5.79855";

$radius="500";

$location=$latitude. "," .$longitude;

$clientid="your client id from FS";

$clientsecret="your client secret from FS";

$version="20110701";

$Client = new ApiClient();

$Client->setMethod(ApiClient::METHOD_GET); // This is not required because the default method is GET

//$Client->setParams(array('client_id' => $clientid , 'client_secret' => $clientsecret, 'll' => $location , 'limit' => '100', 'radius' => $radius));

$Client->setParams(array('client_id' => $clientid , 'client_secret' => $clientsecret, 'll' => $location , 'limit' => '5', 'v' => $version));

//$result = $Client->request('https://api.foursquare.com/v2/venues/trending';, ApiClient::CONTENT_JSON);

$result = $Client->request('https://api.foursquare.com/v2/venues/search';, ApiClient::CONTENT_JSON);

//print_r ($result);

// Show results

echo '<ul>';

foreach($result->response->venues as $item) {

echo '<li>';

echo $item->name ."checkins : " .$item->stats->checkinsCount;;

//echo "Checkins: " .$item->stats->checkinsCount;

echo '</li>';

}

echo '</ul>';

} catch(Exception $e) {

echo 'Foursquare seems to be offline';

}

C
cgphp 7/15/2011

Jquery in PHPrunner is not a pain. This is the right way to return the html data:

$html = '<ul>';

foreach($result->response->venues as $item) {

$html .= '<li>';

$html .= $item->name ."checkins : " .$item->stats->checkinsCount;

$html .= '</li>';

}

$html .= '</ul>';



echo $html;


Don't use print_r(), only echo.

B
bluedude author 7/17/2011

Well, this does not really help because it is the same code in the end. I'm not using print_r, that is just for debugging and it commented out. The problem is that it gives jQuery errrors that jQuery isn't defined.
As said, sample code works fine in my HTML test page (without PHPRunner) so that isn't the problem at all.
Thanks.

Sergey Kornilov admin 7/17/2011

bluedude,
if you have a valid support contract post your application to Demo Account and open a ticket at http://support.xlinesoft.com sending your Demo Account URL. 'Demo Account' button can be found on the last screen in the program.

C
cgphp 7/17/2011



Well, this does not really help because it is the same code in the end.


It is not the same code in the end. You use multi echo instead of only one. Anyway, when you get the “$ is not defined" error, it’s almost always because the jquery.js file has not been included or your reference to it is pointing to the wrong location.