?
Solved

Can I have a form action that does nothing

Posted on 2003-03-12
11
Medium Priority
?
1,035 Views
Last Modified: 2010-10-05
I want to declare my form to do nothing when I submit.

When I hit the return key while in some text editor, the form is submitted even though I really don't want it to be submitted. (Hitting a button to manually move to a new url works fine)

So I need something like

<form method=get action="doSquat" >

but I don't want an error either.

Or how do I prevent the form from submitting when I hit the return while in a text editor?
0
Comment
Question by:rfr1tz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 11

Accepted Solution

by:
LordWabbit earned 800 total points
ID: 8119323
you can add an onsubmit event to the form tag
<form method=get action="doSquat" onsubmit="return wabbit()">
and then have a simple function which returns false
<script language="javascript">
function wabbit(){
  return false;
}
if you want the form submitted later simply place a condition around the return so as to return true instead.

OR

you could put a onkeypress event in the text box and then cancel the keypress event if enter is pressed.
0
 
LVL 14

Expert Comment

by:avner
ID: 8119333
If the form is submitted when you click enter, that means you have a SUBMIT button :
<input type="submit"..> or <input type="image"...>

Just change this to :

<input type="button" onclick="this.form.submit()"/>

Then, when you click enter, the form will not submit itself.


0
 
LVL 11

Expert Comment

by:LordWabbit
ID: 8119391
avner - I tried out your suggestion

<form action="imgoinghere.htm" method="post">
<input type="text" name="login">
<br>
<input type="button" onclick="this.form.submit()" value="submit">
</form>

and it doesnt seem to work as you suggested, the event is fired from the text box and the text box responds to it, submitting the form.  In fact if you remove the submit button entirely the form still submits on enter.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:avner
ID: 8119461
LordWabbit , your example is incorrect, you tested a specific case that there is only one element (with "name" attribute) in the form. Only in such, the form will submit without having a submit button.
Try this :
<form action="imgoinghere.htm" method="post">
<input type="text" name="login">
<input type="text" name="login2">

<br>
<input type="button" onclick="this.form.submit()" value="submit">
</form>
0
 
LVL 11

Expert Comment

by:LordWabbit
ID: 8119507
avner - I tried out your suggestion

<form action="imgoinghere.htm" method="post">
<input type="text" name="login">
<br>
<input type="button" onclick="this.form.submit()" value="submit">
</form>

and it doesnt seem to work as you suggested, the event is fired from the text box and the text box responds to it, submitting the form.  In fact if you remove the submit button entirely the form still submits on enter.
0
 
LVL 14

Expert Comment

by:avner
ID: 8119528
LordWabbit , always click Reload Question, do not refresh the window.
0
 
LVL 11

Expert Comment

by:LordWabbit
ID: 8119539
my mistake avner, your example works perfectly.
sorry about the double post thingy
0
 
LVL 14

Expert Comment

by:avner
ID: 8119570
No problems. Welcome to EE.
0
 
LVL 1

Expert Comment

by:webandit
ID: 8120058
<form method=get action="wherever you want.com" onSubmit="return false;">
<input type="submit">
</form>

is all you need.  You don't have to give away that many points for simple questions.  It's nice of you, but not necessary.
0
 
LVL 4

Expert Comment

by:anderson22
ID: 8121320
two problems:

first you want to stop a manual form submission which is accomplished by catching the onsubmit event and returning false.

secondly, you want to prevent an enter key submission.  This one is a little tricky because every browser allows different methods of submitting forms via enter key (namely, IE will only allow if only one text box is used...supposedly).

Here is a javascript function to use on text boxes onkeypress event handler as well as the onsubmit handler for the form tag:


function checkEnter(e){ //e is event object passed from function invocation
var characterCode  literal character code will be stored in this variable

function checkEnter( characterCode )
{
  if(characterCode == 13)
  {
    return false //return false to the event handler
  }
  else
  {
    return true //return true to the event handler
  }
}

<input type="text" onKeyPress="return checkEnter(event)">


This was stripped from the following article (very comprehensive):
http://www.jennifermadden.com/162/examples/stringEnterKeyDetector.html
0
 
LVL 3

Author Comment

by:rfr1tz
ID: 8121551
avner,
I do not have a "submit" (I swear) yet the form submits when I hit enter while in the editor. However, the editor immediately precedes a button. The form did not submit when I temporarily had an intervening editor between the two. And it's a password editor. I don't know if that makes a difference.

webandit,
your solution is best, but it is only a slight modification of LordWabbit's. So he gets the points. As for the 200 points, I've got more money than brains. It was an easy answer, but I didn't think of it.

Thank all of you for your help.
0

Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question