Title: Documenting prototype.js for AJAX
Author: Alex Kirk
Published: October 5, 2005
Last modified: October 9, 2005

---

# Documenting prototype.js for AJAX

October 5, 2005

As the [prototype.js](http://prototype.conio.net/) library lacks documentation and
I recommend to use the [down cut version for AJAX](https://alex.kirk.at/2005/10/05/prototypejs-just-for-ajax/),
I thought it might be useful to document how to use the AJAX related functions.

**A “normal” AJAX callback**

When you just want to do a post-back to the server (for example to store some data
the user has just changed) you’d use the following piece of code:
 `

A simple corresponding PHP script (store.php) would look like this:
 ` < ?php if(!
isset($_POST["value"])) { die("no value given!"); } $db = mysql_connect(); mysql_select_db("
test"); mysql_query("INSERT INTO table (value) VALUES ('" + addslashes($_POST["value"])
+ "')"); mysql_close(); echo "successful"; ?>

The value the user entered in the prompting box will be stored to a MySQL database.
The magic happens in line 2 of the function store(): `new Ajax.Request` will do 
an HTTP POST to the given script at the first parameter (store.php) with the parameters
given in the second argument. The yet unusual notations within curly brackets denotes
a hash (also called associative array) that stores key/value pairs. Here the pair`
parameters` (key) and `'&value=' + value` (value) are stored. Other possible key/
parameter functions can be found at the [preliminary documentation of prototype.js by Sergio Pereira](http://www.sergiopereira.com/articles/prototype.js.html#Ajax.options).

This brings up a problem common to AJAX applications: you explicitly need to inform
the user about what happened, or if it failed. This can be done via an Event handler.
The most useful one might be `onComplete`. It is inserted like this:

`
 function store() { value = prompt("Give me a value, please."); new Ajax.Request("
store.php", { parameters: '&value=' + value, onComplete: function (req) { if (req.
responseText == "successful") { alert("Your value has been stored successfully");}
else { alert("Something went wrong when storing your value"); } } }); }

This will display an appropriate alert box depending a successful status message
by the script. In near future I will describe how to use the `Ajax.Updater` feature
which allows to easily modify your existing page to instantly display results of
a query.

ajax, introduction, tutorial, prototype.js

[Ajax](https://alex.kirk.at/category/code/ajax/), [Code](https://alex.kirk.at/category/code/)

Read this next

[prototype.js just for AJAX](https://alex.kirk.at/2005/10/05/prototypejs-just-for-ajax/)

## 3 responses to “Documenting prototype.js for AJAX”

 1.  ![clem Avatar](https://secure.gravatar.com/avatar/17ffcf6c838360fc0f9fa6b7384b9ebdf5446e33234c947becf36372db3de4d1?
     s=48&d=mm&r=g)
 2.  [clem](http://labs.beffa.org/)
 3.  [October 9, 2005](https://alex.kirk.at/2005/10/05/documenting-prototypejs-for-ajax/comment-page-1/#comment-15)
 4.  thanks for the example. Should be btw.
 5.  [Log in to Reply](https://alex.kirk.at/wp-login.php?redirect_to=https%3A%2F%2Falex.kirk.at%2F2005%2F10%2F05%2Fdocumenting-prototypejs-for-ajax%2F)
 6.  ![clem Avatar](https://secure.gravatar.com/avatar/17ffcf6c838360fc0f9fa6b7384b9ebdf5446e33234c947becf36372db3de4d1?
     s=48&d=mm&r=g)
 7.  [clem](http://labs.beffa.org/)
 8.  [October 9, 2005](https://alex.kirk.at/2005/10/05/documenting-prototypejs-for-ajax/comment-page-1/#comment-16)
 9.  the script anchor should be with type=”text/javascript” and not text.
 10. [Log in to Reply](https://alex.kirk.at/wp-login.php?redirect_to=https%3A%2F%2Falex.kirk.at%2F2005%2F10%2F05%2Fdocumenting-prototypejs-for-ajax%2F)
 11. ![Alexander Kirk Avatar](https://alex.kirk.at/wp-content/uploads/sites/2/2025/
     06/cropped-2025.Alex-512x512-1-48x48.jpg)
 12. [Alexander Kirk](http://alex.kirk.at/)
 13. [October 9, 2005](https://alex.kirk.at/2005/10/05/documenting-prototypejs-for-ajax/comment-page-1/#comment-17)
 14. Yes of course, thank you.
 15. [Log in to Reply](https://alex.kirk.at/wp-login.php?redirect_to=https%3A%2F%2Falex.kirk.at%2F2005%2F10%2F05%2Fdocumenting-prototypejs-for-ajax%2F)

### Leave a Reply 󠀁[Cancel reply](https://alex.kirk.at/2005/10/05/documenting-prototypejs-for-ajax/comment-page-1/?output_format=md#respond)󠁿

Only people in [my network](https://alex.kirk.at/friends/) can comment.

This site uses Akismet to reduce spam. [Learn how your comment data is processed.](https://akismet.com/privacy/)