Prerequisite: knowledge of Javascript, PHP, XHTML
Difficulty: Intermediate
What you learn:
- Validate form using regular expression
- Make Ajax calls
- Display messages when Ajax call Sent and Received
Software to install: WAMP
Simple Guest Book Screenshots


Live Demo –
http://www.fxtremecoder.com/tutorial/simpleguestbook/simpleform.html
SourceFiles –
http://www.fxtremecoder.com/tutorial/simpleguestbook/xml_http.zip
I am not going to tell you how to set up WAMP and how to put all the sourcefiles in to virtual directory. Assumption is that you already know.
Ok let us get started – I have commented most part of the code, I am just highlighting the main points
please open the file simpleform.html and look for the source
A form tag with no action
A button named submit with onclick event to call for a function addForm Elements
If you look for script tag right at the top of the source code you will find the function addForm Elements. The first thing is to validate the form elements
data_added_in_form = validateForm(document.form1);
here “data_added_in_form” receives a boolean value. if you want to validate form use regular expression. They are the best and the easiest way to validate forms. Look for the function validateForm (it requires the input as the document’s current formname)
I have defined 3 variables
- space
- alpha
- regMail
which contains regular expression values.
“test” is the keyword, rather called inbuilt function to validate your form elements with respect to the above 3 regular expression defined
- space.test with validate for any space or blank information
- alpha.test will test for alphanumeric values
- regMail.test will validate for your email id
when the “data_added_in_form” receives the value as true, all form elements value are concatenated to the variable “data”
data =name +”-”+ email +”-”+ comments +”-”+subscribe;
if you have noticed I have used “-”, this is inorder to seperate the data for processing when data is sent.
Once the data is concatenated, we make the ajax call or send a request to the “xml_http.php” by using the function “sendReq()”
http.open(‘get’, ‘xml_http.php?data=’+data + “&date=”+ date);
the above code will send data to xml_http.php, if you notice I have added date along with it so as to avoid browser cache, so everydata which is sent from your browser is unique
So let us take a insight on the what xml_http.php does
$received_value = $_REQUEST['data'];
Here we receive the data which is sent by simpleform.html, after which we seperate the data to useful information by using the keyword “explode” equivalent to “split” in javascript with requires as comparison string, in my case I have used “-” so all data is stored in “$dat_array” as array. we know the order in which we sent, so it the same manner we start putting the values back to variables
- $name =$dat_array[0];
- $email =$dat_array[1];
- $comments =$dat_array[2];
It is up to you, whether you want to put the received information to your database. In my case I am sending the value inorder to display the same on “simpleform.html”
now with the “echo” keyword I just have to pass the result back to “simpleform.html”
Now let us get deeper to understand how we receive the value in to “simpleform.html”
http.onreadystatechange = hndlResponse;
the above line of code will call the function hndlResponse when the “xml_http.php” gives back the result
What does hndlResponse function do?
before we start let us know the various attributes contributing they are
readyState:
the code successively changes value from 0 to 4 that means for “ready”.
status:
200 is ok
404 if the page is not found.
responseXml:
holds a Xml loaded file, DOM’s method allows to extract data.
responseText:
holds loaded data as a string of characters.
onreadystatechange
property activated by events. A function must be assigned to it.
Now we look in to the code again
responsetext = http.responseText;
If you notice I have used “responseText” beacause I need them as string text, if you were to parse XML data use responseXML
Now the response is stored in the variable responsetext
All you need to do is
document.getElementById(‘commentsa’).innerHTML += responsetext;
so the div element “commentsa” receives the html table element and renders in the browser.
This is how you make a simple Guest Book application and one more thing I have left intentionally is that the comments coloumn always remain empty when loaded, use a php code to get data from DB and fill the comments are with the code
document.getElementById(‘commentsa’).innerHTML += responsetext;
If you like this tutorial, do refer to your friends and do make a comment, so that I can improve more and contribute more to the developer’s world
testing bo’
thx
Comment comment comment
hola can you show us
Can you please upload the script somewhere else, the page you submitted is something else.
Sorry Folks,
The site is no more available will drop the files at a new site and update the page, give me little time
Hi! Why I can’t fill my info in profile? Can somebody help me?
My login is Kisakookoo!
Hello
I want to all of you know, World is mine, and yoursite good
G’night
Thank You
the files aren’t available anymore … could you please post a new downlodable link.
Thanks
Hi Jim. Photos i received. Thanks
the links demo and source can not get any thing. pls make it available friend.
Hehe just cheking this oit
testing
http://www.fxtremecoder.com/tutorial/simpleguestbook/xml_http.zip
really suck the domain is expired
Hello is this script working?
Hello
Good article.
IT TNS Testing #1
tessss
Hello!,
none of the links are available!
kll;l
test
Does htis work
I noticed that this is not the first time you mention this topic. Why have you chosen it again?
p.s. Year One is already on the Internet and you can watch it for free.
asdasd
Please email me with a few hints on how you made this website look this cool, Id be appreciative!