...

View Full Version : jQuery - "return false" when submitting form? (stopping the submit)



nikee
08-25-2010, 07:58 PM
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 (http://vimeo.com/4173282) (even though i'm not making a login script), and it seems to be working for him.

Here's my form:



<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:


$(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.

gizmo1650
08-25-2010, 08:32 PM
<form action="index.php" method="post" name="pForm" onsubmit="return(false)">

nikee
08-25-2010, 08:40 PM
That did it! But i'd like to know how to do it without using inline javascript code.

Thanks! :)

gizmo1650
08-25-2010, 10:47 PM
<form action="index.php" method="post" name="pForm" onsubmit="someFunction()">

Spudhead
08-26-2010, 02:00 PM
gizmo1650 - that's still inline.

nikee - give your form an ID and use that:


<form action="index.php" method="post" id="pForm">


$(document).ready(function() {
$('#pForm').submit(function(){
return false;
});
});

nikee
08-26-2010, 09:20 PM
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! :)

bdl
08-26-2010, 10:25 PM
Don't forget the jQuery Event object preventDefault() method (http://api.jquery.com/event.preventDefault/), e.g.


<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.
});
})



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum