Tutorial: Simple form-submit using AJAXSeite 3 von 3
3. Get AJAX work
Now its time to get AJAX work. Please download my ajax.js.zip file, unzip it inside your document-root. The Javascript-file is well documented, so if you are interessted in, you should not have any problem to understand what is happening.
Include it in your page-header:
If you open the ajax.js file you will see, that the submitAjax function add a post-var named action with the value ajax to the request. We will need this on php-side.
On top most of your php-file add the following lines:
Have a look at the exit at the end of these lines. It is very important to leave the file here. If not, the whole page will be sent back to AJAX and shown in your textarea field. The rest of these lines should not need to be described.
If nothing went wrong, your little script should work now. If javascript is enabled, you should see the wait-message in the textarea and after one second, there should be the AJAX-answer. And… you should not see any page-reload, its AJAX!
If you disable javascript in your browser, you will see a normal HTTP_POST with a page-reload, but the function should work anyway.
For any reason, if it does not work, download my script and find out what was missing.
Stephan
| Description | Type | Filesize | Link |
|---|---|---|---|
| Java-Script file for this tutorial | .ZIP | 1,74 KB | ajax.js.zip |
| PDF Tutorial | 111 KB | ajax-tutorial.pdf | |
| All tutorial files | .ZIP | 107 KB | simple-ajax-tutorial.zip |









Antwort schreiben