Hi,
I am very new in ajax and i found that most of the sample codes are hard for me to understand. Can anyone help me by providing some example or simple codes to submit a form and do some processing then display the next form?
Hi,
I am very new in ajax and i found that most of the sample codes are hard for me to understand. Can anyone help me by providing some example or simple codes to submit a form and do some processing then display the next form?
This book rocks at only $40 + 30% off at Borders -> Head First Ajax
I must say, the PHP aspect of AJAX is often the easiest part. You really need to be proficient in javascript. You can use any server-side language to produce the content (which is where PHP comes in.) Fundamentally a AJAX process works like this:
An onclick or other javascript event is fired.
The handler is specified (ie. <div onclick="handleclick()">do something</div>)
The function handleclick either calls a wrapper function (like Ajax.Request from prototype library) or ultimately XMLHttpRequest. (XMLHttpRequest is the javascript object that makes it all possible.)
Usually, a url is built dynamically in javascript (ie. the search parameter in a text box perhaps.)
A XMLHttpRequest method is called (ie. XMLHttpRequest.open("get", "url.php?search="+document.getElementByID('searchbox').value)...again, a wrapper function hides the complexity of the XMLHttpRequest object and is recommended. I like Scriptaculous together with Prototype....very easy to use!)
XMLHttpRequest.send() is called.
To the receiving PHP script, the transaction looks just like a browser request. PHP can handle it accordingly. the difference is the output is sent back to javascript instead of being displayed in the browser. PHP doesn't know the difference.
The javascript usually has a function "registered" to listen for the return transaction and you write code to handle the data returned. Again, the data can be plain text or whatever the php script returns. Often JSON formatted content is returned which can be evaluated by javascript and easily parsed.
That's basically it. Here is a tutorial that I googled to give you some code examples. Keep in mind, PHP5 now supports converting data to JSON code (A good use is to take a PHP array and convert it to JSON javascript code, which is kind of like converting a PHP array to a Javascript array. Very useful for AJAX.)