Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts

    Question jQuery - "return false" when submitting form? (stopping the submit)

    Hey there.

    I'm running into a bit of trouble while trying to cancel the submit of a form. I've been following this tutorial (even though i'm not making a login script), and it seems to be working for him.

    Here's my form:

    Code:
        <form action="index.php" method="post" name="pForm">
            <textarea name="comment" onclick="if(this.value == 'skriv här...') this.value='';" onblur="if(this.value.length == 0) this.value='skriv här...';">skriv här...</textarea>
            <input class="submit" type="submit" value="Publicera!" name="submit" />
        </form>
    And here's the jquery:
    Code:
    $(document).ready(function() {
        $('form[name=pForm]').submit(function(){
        
            return false;
            
        });
    });
    I've already imported jQuery in the header and i know it's working. My first thought was that it might be outdated, but it's actually "just" a year ago.

    So do anyone see what's wrong?

    Thanks in advance.

    EDIT: From what i've read the easiest and most appropriate way to abort the submit is to return false? But i can't seem to get it working. I've searched the forum and i've found several helpful threads but none of them actually works. I must be screwing something up.
    Last edited by nikee; 08-25-2010 at 08:23 PM.

  • #2
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts
    <form action="index.php" method="post" name="pForm" onsubmit="return(false)">

  • #3
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts
    That did it! But i'd like to know how to do it without using inline javascript code.

    Thanks!

  • #4
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts
    <form action="index.php" method="post" name="pForm" onsubmit="someFunction()">

  • #5
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    gizmo1650 - that's still inline.

    nikee - give your form an ID and use that:

    Code:
    <form action="index.php" method="post" id="pForm">
    Code:
    $(document).ready(function() {
    	$('#pForm').submit(function(){
    		return false;
    	});
    });

  • #6
    Regular Coder
    Join Date
    Jan 2009
    Posts
    173
    Thanks
    5
    Thanked 12 Times in 11 Posts
    Thanks guys. I found the error, and it was such as simple mistake! Kind of embarrassing tbh...

    I had added one too many "});" further down the code, and that was causing it not working.

    Thanks though!

  • #7
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Don't forget the jQuery Event object preventDefault() method, e.g.
    Code:
    <form id="theForm">
      <input type="text" id="username" name="username"/>
      <input type="submit" id="subBtn" name="subBtn"/>
    </form>
    
    $(function(){
      $("form#theForm").submit(function(evt){
        evt.preventDefault();
        // JS code here to filter form data, etc and process.
      });
    })


  •  

    Posting Permissions

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