TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Javascript, AJAX, E4X (http://www.talkphp.com/javascript-ajax-e4x/)
-   -   A Few AJAX Questions (http://www.talkphp.com/javascript-ajax-e4x/1327-few-ajax-questions.html)

Gurnk 10-22-2007 10:32 PM

A Few AJAX Questions
 
Hey. I just have a few general questions about AJAX, as I am thinking about adding a little to my site. So here they are.

I have all my stuff stored in a database, is it easy to update info from a database and not have to reload the page. i.e click a link and it updates a section with the new database stuff. If it is, do you have any example code you could throw my way? Thanks.

EDIT: Also, is it possible to keep all this AJAX (the stuff mentioned above) on the same page, so I don't have to include external files. I've seen some things that might have worked, but they had to post stuff to another file.

Um... Just 1 I guess. :p Thanks guys.

Wildhoney 10-22-2007 11:08 PM

To answer them in order! It's definitely possible to have it update your database without reloading. As for an example, really you'll want to download the popular Javascript framework, Prototype, and give that a whirl with its AJAX features. As well as all the other features it has to offer! It's really easy to use with AJAX. It will give you plenty of examples on the Prototype website.

You don't really have to include external files anyway - just your external Javascript file, although technically this could also be incorporated in your PHP file - just not very coder-friendly. The PHP file that you call when you make your AJAX call can also feasibly be the same file you make the call from - it's just you'll have to configure it a different route of execution for when the page loads when you're accessing it via AJAX as to when you normally access it - you can do this by setting a URL parameter, for instance. However, I load separate files to make the code more easy to manage.

Gurnk 10-22-2007 11:10 PM

Thanks for the quick reply Wildhoney. I'll definitely check Prototype out. I've seen you guys talking about it on the boards before, and was thinking about checking it out anyway.

CMellor 10-23-2007 02:03 AM

I agree with Wildhoney on using prototype. Before I knew that existed, I was doing AJAX the old fashioned way and I hated it! So much code, and it's not guaranteed to work on all browsers, which is a real hassle.

A simple AJAX call in Prototype is very simple:

Code:

function updateForm(element) {
  new Ajax.Request({
    parameters: Form.serialize(element)
  });
}

Which you'd use with an onsubmit attribute

Code:

onsubmit="updateForm(this); return false"
Make sure the form has an ID, otherwise it won't work.

This is pretty useless code, but it gives somewhat of an idea on how simple it is.

Gurnk 10-23-2007 11:30 AM

That is simple. :)


All times are GMT. The time now is 05:12 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0