Quick AJAX example

There are many ways to make the ajax request. This probably isn't the best, but it's the one I am most comfortable using. This example requires that you have the json extension for php. It can be done without it, but json makes the data transfer from php to javascript much cleaner.

testAjax.tpl:

{zoop_file_js files="ajax/dojo/dojo.js"}
<form id="main_form" name="main_form" method="POST" action="{$VIRTUAL_URL}" enctype="multipart/form-data">
<script type="text/javascript">
{literal}
function submitForm(action)
{
    if(window.verifyFormFunction)
    {
        if (!verifyFormFunction(action))
            return false;
    }
 
    document.main_form.actionField.value = inAction;
    document.main_form.onSubmit();
    document.main_form.submit();
}
 
function checkUserName()
{
    dojo.io.bind({
        {/literal}
        url: "{$SCRIPT_URL}/checkUserName",
        {*url: "{$zoneUrl}/checkUserName",*}
        {literal}
        mimetype: 'text/json',
        method: 'post',
        formNode: document.main_form,
        handler: function(type, data, e){handleUsernameData(data);}
    });
}
 
var usernameOk = false;
function handleUsernameData(data)
{
    if(data.exists == true)
    {
        div = document.getElementById('usernameExists');
        div.style.display = '';
    }
    if(data.bad == true)
    {
        div = document.getElementById('usernameBad');
        div.style.display = '';
    }
    if(data.ok == true)
    {
        div = document.getElementById('usernameExists');
        div.style.display = 'none';
        div = document.getElementById('usernameBad');
        div.style.display = 'none';
        usernameOk = true;
    }
}
 
function verifyFormFunction(action)
{
    if(action == 'submit' && !usernameOk)
    {
        alert('Please enter a valid username.');
        return false;
    }
    return true;
}
{/literal}
</script>
 
<input type="text" name="username" onBlur="checkUserName();">
<div id="usernameExists" style="color: red; display:none">This username already in use.</div>
<div id="usernameBad" style="color: red; display:none">This username is too short, or contains invalid characters.</div><br/>
<input type="password" name="password"><br/>
<input type="password" name="password2"><br/>
<input type="submit" onclick="submitForm('submit'); return false;">
<input type="hidden" name="actionField" value="default">
</form>

zone_default.php:
function pageTestAjax($p)
{
    $this->guiDisplay('testAjax.tpl');
}
 
function postTestAjax($p)
{
    //save the user
}
 
function postCheckUserName($p)
{
    $username = getPostText('username');
    $answer = array();
    if(strlen($username) < 2)
        $answer['bad'] = true;
    else
        $answer['bad'] = false;
    if(sql_check("select id from user where username = '$username'"))
        $answer['exists'] = true;
    else
        $answer['exists'] = false;
    $answer['ok'] = !$answer['bad'] && !$answer['exists'];
    echo json_encode($answer);
}

Header Missing

I also send a header with my JSON data which will help Prototype decode the data automatically. I am not sure it this helps DOJO or not. Just thought I would add this if others needed to know

function postCheckUserName($p)
{
    $username = getPostText('username');
    $answer = array();
    if(strlen($username) < 2)
        $answer['bad'] = true;
    else
        $answer['bad'] = false;
    if(sql_check("select id from user where username = '$username'"))
        $answer['exists'] = true;
    else
        $answer['exists'] = false;
    $answer['ok'] = !$answer['bad'] && !answer['exists'];
    //Header to indicate JSON data
    header('Content-type: application/x-json');
    echo json_encode($answer);
}

jmorant@cloud9l... 23 Jul 2008

Bad

The example is not working correctly, fix it. It`s not a good idea to post something witch not working properly.

Look at this : $answer['ok'] = !$answer['bad'] && !answer['exists'];
what is this????????????

some81 31 Oct 2008

Typo

Thank you for pointing out that typo.

john 31 Oct 2008

The rasmus lerdorf's really

The rasmus lerdorf's really long talk at drupalcon szeged last year make sure to check out the presentation slides for all the gory details. Thanks for sharing the informative post.
Regards.
samson - car ship

samson77 18 Feb 2011

This is good site to spent

This is good site to spent time on .I just stumbled upon your informative blog and wanted to say that I have really enjoyed reading your very well written blog posts. I will be your frequent visitor, that’s for sure..
Medical billing || NJ nightlife

steve 31 Mar 2011

thanks

This is a very informative article.I was looking for these things and here I found it.Masterboard I am doing a project and this information is very useful me. If you are interested, but this is my duty to inform you that virtual administrative assistant a very dedicated service and can be applied anywhere you want and get better results.

hushcat 15 Apr 2011

I enjoyed reading this post

I enjoyed reading this post which is highly informative.I recently came across your blog and have been reading along and the article you have written is very interesting.
mayweather vs ortiz tickets | home lighting ideas

zarahfulton 13 Jul 2011

Great Tutorial

This reminds me of the dreadful programing days back in college.Small boat building It still refreshing to see that other don't find it as difficult as I. Cheers & keep the programing alive.

Donni 28 Jul 2011

Nice after having been with

Nice after having been with this interesting work. What's really good to know that this issue is also being covered on this site, so thanks for taking the time to discuss this. Julian @ Fleet Tracking

JulianAnd 30 Aug 2011

nice blog

I appreciate, lead to I found exactly what I used to be having a look for. You have ended my four day lengthy hunt! God Bless you man. Have a nice day. Christmas countdown

Bhatt0 05 Sep 2011

Das Konto testen. Wie

Das Konto testen.
Wie Tagesgeld funktioniert.
Einfach Tagesgeld vergleichen.
Wieso Geldanlagen gefragter werden.

kiooo 07 Sep 2011

hi

That Mercedes Benz vehicle was already famous as a luxury vehicle a very classy. I think it fair if this car is the right choice.how to get your ex back | yeast infection treatment

vang1234 13 Sep 2011

I am very happy for finding

I am very happy for finding this another ajax technique example.I will use this in my application. I am sure that your job is clearly identified as a fair game. template website

vildi 10 Nov 2011

Youre truly well-informed. I

Youre truly well-informed. I cant believe how much of this I just wasnt aware of. Thank you for bringing more information to this topic for me. Im truly grateful and really impressed.relatiegeschenken

leonardod 09 Jan 2012

This is really awesome!

devil 12 Jan 2012

Whenever possible, because

Whenever possible, because you gain expertise, do you mind updating your blog post with extra information?Computerhulp

leonardod 15 Jan 2012

software

I wonder if you could help me with a dll search sequence which could also work attached to a website plugin so any user can have access to it. Could you help me with a code example for my issue? I would appreciate that. Thank you.

garycopper 20 Jan 2012

A bit hard

Your question is a bit hard for me..maybe someone will help you.
pizza games

ghose 21 Jan 2012

Good Infomation

keep up the good job i like your post very much. expect more post from your side. waiting for your post. keep up the good job

http://www.zithromaxgeneric.net

ozone mattew 01 Oct 2011

Yes really such a nice

Yes really such a nice tutorials and explanation Web design

seoindia 17 Nov 2011

Austin Home Builder

The faster the code can run, the better the performance. I think programmers often get caught up using complex algorithms, and that often leads to a slower system. The end user is mainly concerned with reliable performance and a fast pace. If you can accomplish those two factors, then you've created a successful program.Austin Home Builder

Lucky127 14 Jan 2012

outsourcing

It’s always nice when you can not only be informed, but also entertained! I’m sure you had fun writing this article.outsourcing

asim 14 Jan 2012

hello

Fabulous post,This is such a great resource that you are providing us.
qr code maker

jesika 17 Nov 2011

The content is very original

The content is very original and interesting. I've got a big pleasure to spend time in your site. Congratulation for your nice work.
free cell phone spy software

rebiccajoseph2 22 Nov 2011

Excellent Example. Resources

Excellent Example. Resources like the one you mentioned here is very useful! I will post a link to this page on my blog. I am sure my visitors will find that very useful.

Johnson 18 Apr 2011

Good examples! Please post

Good examples! Please post more helpful examples so I shall be able to work on php more effectively. Thanks for kind help :)
Brochure Design

Johnny 22 Apr 2011

This is a great inspiring

This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information.
California DUI Lawyer

peter09 21 May 2011

AJAX EXAMPLE

I had used several other ways to make an ajax request but all of those failed. This is the only one that I found to be very comfortable and I guess the best among all other examples that I had found. Thank you so much for this. Golf Clubs Second Hand

chellax 27 Jun 2011

Web applications have made

Web applications have made huge leaps and bounds in improving user experience thanks to a large number of newly developed technology Ajax.
Jullian @ LED TV deals

JulianAnd 07 Sep 2011

Eine Kreditkarte testen. Wie

Eine Kreditkarte testen.
Wie die Kreditkarte verglichen wird.
Wie Kostenlose Kreditkarten gefunden werden.
Ein Konto finden.
Wie man Kreditkarten vergleicht.

kiooo 07 Sep 2011

Web Design Dubai

OMG! I have to say, your blog needs to be one of the best written blogs that I have learn in a protracted time.What I wouldn't give as a way to put up posts which might be as interesting| as yours. I guess I will have to continue studying yours and pray that someday I will write on a subject with as a lot wisdom as you have! Bravo!! Web Design Dubai

ali.shass 06 Jul 2011

I recently came across your

I recently came across your article and have been reading along. commercial air filters

asim 11 Sep 2011

Thank you for another

Thank you for another essential article. Where else could anyone get that kind of information in such a complete way of writing? I have a presentation incoming week, and I was on the lookout for such information.Waterproofing basement

asim 02 Oct 2011

Thanku for this example..

Really thanks for such article.I am very happy for finding this another ajax technique example.I will use this in my application.This example would be more helpful for the developer.
buy used cars

keshav 11 Oct 2011

Hello, I truly enjoyed

Hello, I truly enjoyed reading your post. I found your site from Bing. Will bookmark to return later. Thanks!
Wedding Bands

sammon 03 Jun 2011

I am very much interested in

I am very much interested in reading articles. This blog was really an awesome site which I had never found it anywhere. Lots of stuff in this site ! Really helpful for most of them ! baldai

Bhatt0 03 Sep 2011

Thanks for enjoying it.

The ZOOP development is great.

lmi pumps

serenali 11 Jun 2011

Kala Darshan

Even i like the zoop development too kaftan

Kaftan 26 Nov 2011

It is such an important

It is such an important topic and ignored by so many, even professionals! I thank you for helping to make people more aware of these issues. Home Automation | Gourmet Gift Baskets

kahanniya 11 Jul 2011

Useful information

Useful information shared..Iam very happy to read this article..thanks for giving us nice info.Fantastic walk-through. I appreciate this postFettabbau effektiv

chinkiseo20 01 Aug 2011

Though you have said that

Though you have said that this wasn't the best example you have but I must try it for me to judge and experience.

GolfSmith

kyla 19 Aug 2011

hello

Just bookmarked this link in my browser. I will definitely visit this site later to read some more quality blogs. Thanks. vacation to Disney

jesika 17 Nov 2011

This is a very intriguing

This is a very intriguing post, I was looking for this knowledge. Just so you know I located your blog page when I was looking around for blogs like mine, so please check out my site sometime and leave me a comment to let me know what you think.Old Cars For Sale

andrewjpeterson 19 May 2011

Haus Haus Haus Haus Haus

kimmolde 08 Jun 2011

Nowadays, there is a trend

Nowadays, there is a trend of getting a website designed and developed every other day, however, most of them do not provide the returns, which they could. Yes, you have read it right, a website can provide you immense potential customers on a daily basis if it is a business web portal, and countless visitors,

ssstechnology2 02 Jun 2011

I would like to voice my

I would like to voice my admiration for your kindness supporting those individuals that really want help with this subject. Your real commitment to getting the message all-around had been particularly interesting and have regularly enabled most people just like me to attain their targets. The important help means a whole lot a person like me and far more to my colleagues. Many thanks; from everyone of us.....

clubmz e-spy

lariamanizo 14 Sep 2011

Yes!

What an enjoyable read this turned out to be! It's not too often I find content that impresses me on every level - and this post most certainly did. Reverse Cell Phone Lookup|Pellet Stoves

john7334 06 Nov 2011

I think you did an awesome

I think you did an awesome job explaining it. Sure beats having to research it on my own. Thanks
phlebotomy training

phlebotomy 03 May 2011

I really want to thank yahoo

I really want to thank yahoo search engine who have sent me to this wonderful site. The articles posted on this site are great and amazing. I am glad that I have found this amazing site.
лечение в израиле

awesomeseo 02 Jun 2011

Wie eine

Wie eine rechtschutzversicherung aussieht.
Den Privat Rechtsschutz vergleichen.

kimmolde 08 Jun 2011