SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    prevent form from submitting

    Hi

    I have the following fucntion, which works. I would like to prevent the submission of the form in case that errorMessage.length is greater then 0. I tried with e.preventDefault, nothing, I try return false...nothing. Form is still submitted.



    Code:
    formLogin.on('submit', function(){
                   $.ajax({
                       type: 'POST',
                       url: 'backOffice/loginCheck',
                       data: $(formLogin).serialize(),
                       dataType: 'json',
                       success: function(errorMessage){ 
                           
                           if(errorMessage.length > 0) {  
                               $('#errorMessageTop').fadeIn();
                               
                           } 
                            return false;
                       }
                       
                   });
                   
        
               });
    Any help?

    Regards,zoreli

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,871
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The problem is that you are not determining if there is an error or not until after the ajax call returns which will be long after the default action will have run so trying to prevent the default from that point is too late.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Stephen

    Can you point me to some solution?

    Regards,zoreli

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I already gave you the solution in your other thread http://www.sitepoint.com/forums/show...=1#post5170887

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    I have the following problem now...if user submit correctly his user name and password, he remain on the submit form...he is not redirected on the login page. In any case, he remain stucked in the login form.

    Regards,Zoreli

  6. #6
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    How are you managing the redirect as your above code doesn't have anything that would redirect a user.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi
    I have the redirect in php file, named loginCheck. Should I redirect the user from javascript in case that errorMessage is empty? Is that the way how it should work?

    Regards,Zoreli

  8. #8
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Ajax requests are client side so any server redirection won't have any effect because the page physically hasn't changed to a new URL, what you will need to do is send back a success/error message and use your JavaScript to run a quick check on the reply back from the server.

  9. #9
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Ugh...any code snippet how that should look like?

    Regards,Zoreli

  10. #10
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    In your PHP code you would have something like the below

    PHP Code:
    function loginCheck() {
        
    $return = array(
            
    'status'  => false,
            
    'message' => 'You\'re login attempt has failed!'
        
    );

        
    $isAuthenticated true;

        
    // Simple login check, this is just an example so you're code will differ
        
    if ($isAuthenticated) {
            
    $return['status']  = true;
            
    $return['message'] = 'You have successfully been logged in!';
        }

        exit(
    json_encode($return));

    Then your JavaScript would look something like this

    Code JavaScript:
    formLogin.on('submit', function(e) {    e.preventDefault();
     
        $.ajax({
            type     : 'POST',
            url      : 'loginCheck',
            data     : $(formLogin).serialize(),
            dataType : 'json',
            success  : function(r) {
                if (r.status) {
                    window.location = 'link-goes-here';
                } else {
                    $('#errorMessageTop').fadeIn();
     
     
                    // You could also use the status message from the response
                    // alert(r.message);
                }
            }
        });
    });

  11. #11
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a ton mate. It works like a charm now. Finnaly.

    Once again, thanks to all that spend their time to answer my questions.

    Cheers.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •