Want HTML form submit to do nothing
By using return false;
in the JavaScript code that you call from the submit button, you can stop the form from submitting.
Basically, you need the following HTML:
<form onsubmit="myFunction(); return false;">
<input type="submit" value="Submit">
</form>
Then the supporting JavaScript code:
<script language="javascript"><!--
function myFunction() {
// Do stuff
}
//--></script>
If you desire, you can also have certain conditions allow the script to submit the form:
<form onSubmit="return myFunction();">
<input type="submit" value="Submit">
</form>
Paired with:
<script language="JavaScript"><!--
function myFunction() {
// Do stuff
if (condition)
return true;
return false;
}
//--></script>
How do get an HTML form submit (and Python function return) to do nothing using Flask
You're not supposed to return the string
"False", but the Boolean value. Actually, if you want to do this 'properly', you'd return server response 204.
You can do this with return Response(status=204)
Of course, you must import that class from flask first.
HTML button to NOT submit form
I think this is the most annoying little peculiarity of HTML... That button needs to be of type "button" in order to not submit.
<button type="button">My Button</button>
Update 5-Feb-2019: As per the HTML Living Standard (and also HTML 5 specification):
The missing value default and invalid value default are the Submit
Button state.
form with no action and where enter does not reload page
Add an onsubmit handler to the form (either via plain js or jquery $().submit(fn)), and return false unless your specific conditions are met.
Unless you don't want the form to submit, ever - in which case, why not just leave out the 'action' attribute on the form element?
How to prevent form from being submitted?
Unlike the other answers, return false
is only part of the answer. Consider the scenario in which a JS error occurs prior to the return statement...
html
<form onsubmit="return mySubmitFunction(event)">
...
</form>
script
function mySubmitFunction()
{
someBug()
return false;
}
returning false
here won't be executed and the form will be submitted either way. You should also call preventDefault
to prevent the default form action for Ajax form submissions.
function mySubmitFunction(e) {
e.preventDefault();
someBug();
return false;
}
In this case, even with the bug the form won't submit!
Alternatively, a try...catch
block could be used.
function mySubmit(e) {
e.preventDefault();
try {
someBug();
} catch (e) {
throw new Error(e.message);
}
return false;
}
Submit button doesn't work
If you are not using any JavaScript for form validation then a simple layout for your form would look like this:
<form action="formHandler.php" method="post">
<input name="fname" id="fname" type="text" value="example" />
<input type="submit" value="submit" />
</form>
You need to ensure you have the submit
button within the form element and an appropriate action
attribute on the form
element is present.
For a more direct answer, provide the code you are working with.
You may find the following of use: http://www.w3.org/TR/html401/interact/forms.html
Prevent users from submitting a form by hitting Enter
You can use a method such as
$(document).ready(function() {
$(window).keydown(function(event){
if(event.keyCode == 13) {
event.preventDefault();
return false;
}
});
});
In reading the comments on the original post, to make it more usable and allow people to press Enter if they have completed all the fields:
function validationFunction() {
$('input').each(function() {
...
}
if(good) {
return true;
}
return false;
}
$(document).ready(function() {
$(window).keydown(function(event){
if( (event.keyCode == 13) && (validationFunction() == false) ) {
event.preventDefault();
return false;
}
});
});
Submit Button didn't do anything
The code isn't working because you are using onsubmit = event.preventDefault()
which prevents the default behaviour of form submission. That's why you are not getting redirected to the result.html
page. Remove the event.preventDefault()
and this should be working
Related Topics
How to Get All Selected Values from <Select Multiple=Multiple>
Html5 Check If Audio Is Playing
How to Dynamically Create '@-Keyframe' CSS Animations
How to Figure Out the Highest Z-Index in Your Document
Prevent Scroll Bubbling from Element to Window
HTML Input Type=File, Get the Image Before Submitting the Form
Check a Radio Button with JavaScript
How to Remove Tinymce and Then Re-Add It
How to Do a Horizontal Scroll on Mouse Wheel Scroll
How to Access Shadow Dom Elements Through the Parent Document
Getting Bootstrap's Modal Content from Another Page
Want HTML Form Submit to Do Nothing
How to Use Greasemonkey to Selectively Remove Content from a Website
Setting CSS Value Limits of the Window Scrolling Animation
Make Function Wait Until Element Exists
Integrating Dropzone.Js into Existing HTML Form with Other Fields