Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > PHP and AJAX

PHP and AJAX
Thread Tools
anilnair
Fresh-Faced Recruit
Join Date: Feb 2008
Status: Offline
Reply With Quote
Feb 21, 2008, 08:36 PM
 
New to AJAX..

I am trying to write a module that uses JS and PHP, and the functions need to be called back and forth with data.. I have installed XAJAX. I have managed to have JS calling PHP functions properly, but, I am not able to get the PHP code to call JS function properly...

If anyone can help, it would be appreciated.. Here is the code snippet:

// Register function
$xajax->registerFunction("markSelectedProject");
$xajax->registerFunction("selectProject");


// JS function to be called from PHP
function markSelectedProject (startLat, startLong, endLat, endLong) {
alert (\"In markSelectedProject\");
return;
}

// PHP function that retrieves data from DB and tries to call JS function...
function selectProject($start, $end) {

$objResponse = new xajaxResponse();

$querySelect = "SELECT * FROM {table} where endLat = $curLat and endLong = $curLong";
$queryResult = db_query_range($querySelect, 1);
$curObj = db_fetch_object($queryResult);

drupal_add_js('markSelectedProject(' . $curObj->StartLat . ',' . $curObj->StartLong . ',' . $curObj->EndLat . ',' . $curObj->EndLong . ')', 'inline', 'footer');
xajax_markSelectedProject($curObj->StartLat, $curObj->StartLong, $curObj->EndLat, $curObj->EndLong);

return $objResponse->getXML();
}
     
Gavin
Mac Elite
Join Date: Oct 2000
Location: Seattle
Status: Offline
Reply With Quote
Feb 23, 2008, 03:45 AM
 
first thing I'd do is enable the debug menu and open the javascript console if you are using safari. firefox has a debugger too.

try to intercept the output from php - maybe add a line to write the result to file so you can check it for errors. or turn on php error logging.

I haven't used XAJAX but it may have some debugging built in.

a quick glance at the code:
$objResponse = new xajaxResponse(); <- creating the object
return $objResponse->getXML(); <- dumping the content as XML

where do you actually put content into the object?
again, I don't know XAJAX but this looks fishy.

I would expect something like:
$objResponse->addSomething('whatever');
You can take the dude out of So Cal, but you can't take the dude outta the dude, dude!
     
Synotic
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Mar 2, 2008, 01:07 PM
 
I think your use of this framework is obscuring the simplicity of Ajax. Really, all you're doing is getting or posting to some HTTP resource (like http://yoursite.com/currentweather.php), and then fetching the result. A benefit of Ajax is that you can route the response to a handler function that has a parameter containing the response (like handle_response(response)).

One complication is that there is quite a bit of code forking when it comes to making an asynchronous call to a remote server, so using something like Prototype isn't a bad idea.

If you used Prototype, your JavaScript might look something like this:

new Ajax.Request('http://yoursite.com/currentweather.php',
    {
        method: 'get',
        onSuccess: function(transport) {
            var response = transport.responseText;
            alert("Success! \n\n" + response);
        }
    }
);

You wouldn't have to do anything particularly special in your PHP. Just do what ever calls you need to get the current weather, and then echo the result. The response will be sent back to the client, your handler function will be called, and the "response" variable will hold the text.

The other thing to keep in mind is that sometimes you might want to give your data a bit more structure (using arrays, dictionaries, etc...). There a few different ways to do this, one is to use JSON (which I prefer for its simplicity; XML is a little annoying to work with prior to PHP5) or XML, which you're currently using. I'll leave the details of this as a homework assignment, but look into responseXML .

Good luck.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 08:05 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,