Link to home
Start Free TrialLog in
Avatar of William-B
William-B

asked on

AJAX post submit actions

Hello,
I am currently writing a script which is supposed to take a user inputted registration form and if some of the information already exists in a database (MySQL) then copy it to a set of different fields on the form.  I am using AJAX to perform the action so the page doesn't have to reload but I am having some problems with it.  I'd also like to be able to return a generic error if something else goes wrong.  I think the code below will explain it better, I'm putting in comments of where I'm trying to make things happen.  My question is; what is the best way to have multiple actions happen?  I've gone through some tutorials but usually it's only a single operation which takes place when the AJAX request is performed.  Thanks in advance and let me know if you need any other information.
P.S. I am using Kohana as a framework, but I don't think that should matter for the actual AJAX programming.

public function action_register()
    {
        if( $this->request->is_ajax() )
        {
            $this->auto_render = FALSE;
        }
        if( $this->request->method() === Request::POST )
        {
          
            $user = new Model_User;
            $post = $this->request->post();


            if( $user->where( 'email', '=', $post[ 'Email' ] )->count_all() === 0 )
            {
                
                $userparams = array();
                $userparams[ 'first_name' ] = $post[ 'FirstName' ];
                $userparams[ 'last_name' ] = $post[ 'LastName' ];
                $userparams[ 'email' ] = $post[ 'Email' ];
                $userparams[ 'emailconf' ] = $post[ 'Emailconf' ]; 
                $userparams[ 'password' ] = $post[ 'Password' ];
                $userparams[ 'passconf' ] = $post[ 'Passconf' ];
                $usercheck = $user->validate_create( $userparams );

                try
                {
                    $usercheck->check();
                    $user->values( $userparams );             
                    $user->save();
                    $user->add( 'roles', ORM::factory( 'role' )->find(1) );
                    Auth::instance()->login( $this->request->post( 'Email' ), $this->request->post( 'Password' ) );
                    $this->redirect( 'userhome' );
                }
                catch( ORM_Validation_Exception $e ) // Registration validation error
                {   
                    // Return an error asking user to confirm correct values (possibly additional processing)
                }
            }
            else // Email exists
            {
                // Copy 'Email' value to 'LoginEmail' form element on main page.
            }

        }
        else
        {
            // Request was not a correct POST request
        }

Open in new window


Script Code:
$(document).ready( function()
{
    $( '#register_form' ).submit( function( e )
    {
        e.preventDefault();
        var fName = document.getElementById( "FirstName" ).value;
        var lName = document.getElementById( "LastName" ).value;
        var Email = document.getElementById( "Email" ).value;
        var Emailconf = document.getElementById( "EmailConf" ).value;
        var Pass = document.getElementById( "Password" ).value;
        var Passconf = document.getElementById( "PassConf" ).value;
        var dataString = 'FirstName=' + fName + '& LastName=' + lName + '& Email=' + Email + '& Emailconf=' + Emailconf + '& Password=' + Pass + '& Passconf=' + Passconf;
        $.ajax(
                {
                    type: "POST",
                    url: "index.php/register",
                    data: dataString,
                    success: function( data )
                    {
                        alert( data );
                        // Separate Email Copy and recheck form
                            
                        }
                    }
                    
                });
    });
});

Open in new window

SOLUTION
Avatar of Ahmed Merghani
Ahmed Merghani
Flag of Sudan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of William-B
William-B

ASKER

Sorry about the delay in responding.  I got it working using all your solutions.  Thanks very much for all your input.  I have run into a problem though, when I debug the return message ("email" or such) it is extremely slow.  I'm running this on a local host so a connection shouldn't be a problem.  It's just when I click "submit" it takes about .5 - 1 second to return from the ajax call.  Does anybody know how to speed this up?  I know it's an asynchronous call, but there has to be some tweaks somewhere.  Thanks again.
you say - "there has to be some tweaks somewhere"
??? ? ?
tweaks to what ?
how to speed up what? ?
you show us nothing we can evaluate, to try and help.
what code processes are you using?  the first browser code, , the second server php code, and the last JS .done code.
If there is a delay in your response it is most likely in your server side process.

You need to look in your register code to see what could be causing the delay.