Javeline / Ajax.org, PHP, Replies
Submit a form with AJAX using TelePort
Yesterday, PHPBuilder posted an article about sending a form using AJAX. This article shows how to post a simple form. But looking closely at the example, you can also see what the problem is using the plain XMLHttpRequest object and writing an implementation yourself.
First of all you to write some rather difficult javascript code. Next you need to completely rewrite your page, looking nor working like a normal HTML form. Last and most important, the function fetches values using getElementById(). This is not really very flexible, because you will need not only the form, but the function as well when you add a field.
Using an AJAX library can really help you here. Javeline TelePort has got a brand new method, which automatically creates a HTTP post request of a form and sends it to the server.
We’ll start with a plain HTML form, without AJAX. Like always, the URL is in the action attribute. To keep the page open, the result will appear in a new window.
[SOURCE::jtp_submitform/without_ajax.html::xml]
See it working
Now lets add some AJAX. We need to include the TelePort library, which is the communication only package of Javeline PlatFrom.
<script src="teleport_sdk/teleport_post.js" type="text/javascript"></script>
AJAX is asynchronous, so we do need to define a function to handle the result of the call. We’ll add a getHttpRes() function which pretty much does the same as the one in the PHPBuilder article. We’ll also add a reset function to place back the form. (Requesting the form with AJAX just doesn’t seem useful.)
function getHttpRes(msg, state, extra){ if(state != __RPC_SUCCESS__) return alert("Could not connect to server"); if (!self.orig_div1) self.orig_div1 = document.getElementById('div1').innerHTML; // Save to original content for reset document.getElementById('div1').innerHTML = msg + "<br/><input type='button' onclick='reset_div1();' value='Try again' />"; } function reset_div1() { if (self.orig_div1) document.getElementById('div1').innerHTML = self.orig_div1; }
Pushing the submit button on the form would still do a normal post, instead of doing an AJAX call. This is however where you can really see the use of using a library. We can simply add a onsubmit event, letting TelePort send the form and then return false to cancel the post event.
<form ... onsubmit="new POST().submitForm(this, getHttpRes); return false;">That all there is to it. Plus a bonus: when javascript is disabled, the form will still work.
[SOURCE::jtp_submitform/with_ajax.html::xml]
See it working
For you see it believing people out there. Here is an example showing a more advanced form. The javascript didn’t need to be changed at all to make this work.
[SOURCE::jtp_submitform/advanced_ajax.html::xml]
See it working
I hope I managed to convince you to use a library for AJAX, instead of doing it all by yourself, preferably Javeline of course, but make up your own mind there.
11 Aug 2007 Arnold Daniels





Great solution!! will use it –
But do you have a thought as to how to
use a human readable page as response to the
action=”requrest.php” target?
As I understand, it is this target that is used both for the
ajax-submit – and for the “javascript turned off” regular submit ?
In that case – my visitors would see some strange xml – output.. ? Do you have an Idea as to how to provide a different target for
js and non js browsers?
regards. and thanks for sharing!
Currently I handle this on the server side. You can check the HTTP_USER_AGENT, which is “Javeline TelePort x.x” for an AJAX call. In PHP you may do
preg_match(’/^javeline\steleport/i’, $_SERVER['HTTP_USER_AGENT'])
Having an optional ajax_action is a good idea. I’ll push for it to be added in a future release.
Great! thanks.
Now I have only one issue – and that is a question of focus.
I use this for autosaving of formcontent. However – when the timer calls the save-function the focus of the keyboard is affected. the submit resets the keyfocus – to the start of the form field. Very annoying when writing blogposts or other long stuff.
Do you have an idea of a favourable solution?
I considered maybe copying the content to an identical form and posting from that – but it does’nt seem as elegant..
Hi Arnold.
Thank you for following up.
It seems this is a bug with tinymce – and not with the submit at all.
Best regards,
Øyvind
Hi Arnold.
Thanks for the information, very useful.
But i’m trying to implement a little box that’s showed while teleport do the job… like the gmail “loading…” red box
Something like this:
http://www.pointwriter.com/blog/index.php?/archives/2-A-GMail-like-Loading-Notifier.html
But seems that teleport is not compatible with this javascript code… can you help me?
Thanks man.
Febox
ThanosEA
Hi
I try to use the new version of teleport for my website but when I test, firebug give this error ‘jpf is not define’.
When I compare with my old version I can’t find the initialisation of jpf as Kernel (Kernel =)
Anyone could help me ?
CU
Sweet! I was about to use my hosting’s default CGI form submit, but this one is soooo much cooler.
Cheers for the info mate.
בגולן
I get: “Sorry,no posts match your criteria” for every example….
using FF3