Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

using AJAX to check username duplication

using AJAX to check username duplication. there's another submit button on form, should not use submit whole page to check username availability.

in template:
        <DIV id='usercheck'>&nbsp;</DIV>
        <?php
 
        echo submit_to_remote('ajax_submit', 'Check Username', array(
            'update'   => 'usercheck',
            'url'      => '/user/checkusername',
        ));

in action class, check dupliation:

    public function executeCheckusername(){
        $username=$this->getRequestParameter('obj_username');
        $c = new Criteria();
        $c->add(UserPeer::USERNAME, $username);
        $user = UserPeer::doSelectOne($c);
 
        if ($user) {        // username already exists
            $this->setFlash('username_exists', 'y');
            return sfView::SUCCESS;
        } else {
            $this->setFlash('username_exists', 'n');
            return sfView::SUCCESS;
        }
 
    }

in checkusernameSuccess, display error message:

    <?php
    if($sf_flash->get('username_exists') == 'y'){ ?>
    <FONT COLOR="RED">Username Not Available</FONT>
    <?php } else { ?>
    <FONT COLOR="GREEN">Username Available</FONT>
    <?php } ?>
by William Duan on 2007-01-07, tagged ajax 

Comments on this snippet

gravatar icon
#1 Quenten Griffith on 2007-01-07 at 09:53

Thank you I was looking for something like this

gravatar icon
#2 Pierre Minnieur on 2007-01-16 at 07:47

I'm sorry to tell you, but for things like that there's already the sfPropel/DoctrineUniqueValidator which you should use in your <form>.yml in validators/.

gravatar icon
#3 Quenten Griffith on 2007-01-22 at 01:14

Pierre, correct me if I am wrong please but to use the validator would require the form to be submited to the server. This code snippet allows you to now submit the whole form and just have an AJAX action check the name against the database prior to submting the whole form.

gravatar icon
#4 earth on 2008-02-25 at 01:23

Thanks..

You need to create an account or log in to post a comment or rate this snippet.