472,345 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,345 software developers and data experts.

Prevent form submission only from default action

I have a form without a submit button like:

<form name="form1" action=""
onsubmit="alert('submit ' + this.name);">
<div>
<label>Field 1: <input type="text" name="field1"
onchange="alert(this.name + '=' + this.value);"></label></div>
<label>Field 2: <input type="text" name="field2"
onchange="alert(this.name + '=' + this.value);"></label></div>
</form>

The form field 'change' event handlers actually perform AJAX
submission and update some part of the page without "navigating away".

Using the exact form I've given the different browsers behave
differently in handling the default action event (pressing the Enter
key), for example using Mozilla pressing Enter in a field doesn't
cause form submission, but Safari submits the form. I've found
Mozilla submits the form in response to pressing Enter if there's
only one visible field, also:

<form name="form1" action=""
onsubmit="alert('submit ' + this.name);">
<div>
<label>Field 1: <input type="text" name="field1"
onchange="alert(this.name + '=' + this.value);"></label></div>
</form>

Is it possible to detect the form is being submitted in response to
the default action event?

--
Stanimir
Mar 19 '08 #1
6 4981
Stanimir Stamenkov wrote:
Using the exact form I've given the different browsers behave
differently in handling the default action event (pressing the Enter
key), for example using Mozilla pressing Enter in a field doesn't cause
form submission, but Safari submits the form. I've found Mozilla
submits the form in response to pressing Enter if there's only one
visible field, also:
I don't have access to Safari, but returning false from onSubmit on the
form doesn't do the trick for you (ie. does not submit the form)? Since
obviously as you don't have submit button, you do not want an old
fashioned submit to happen, right?

Regards, Tumi
Mar 19 '08 #2
Wed, 19 Mar 2008 12:07:33 +0200, /Tuomo Tanskanen/:
I don't have access to Safari, but returning false from onSubmit on the
form doesn't do the trick for you (ie. does not submit the form)? Since
obviously as you don't have submit button, you do not want an old
fashioned submit to happen, right?
The form is still submitted but through an AJAX library (in response
to field 'change' change events, in my case). Unconditionally
returning false from 'submit' handler prevents the library from working.

--
Stanimir
Mar 19 '08 #3
Stanimir Stamenkov wrote:
Wed, 19 Mar 2008 12:07:33 +0200, /Tuomo Tanskanen/:
>I don't have access to Safari, but returning false from onSubmit on
the form doesn't do the trick for you (ie. does not submit the form)?
Since obviously as you don't have submit button, you do not want an
old fashioned submit to happen, right?

The form is still submitted but through an AJAX library (in response to
field 'change' change events, in my case). Unconditionally returning
false from 'submit' handler prevents the library from working.
Yes, but doing it like

<form action="fallback.php" method="post" onSubmit="yourAjaxHandler();
return false;">

or even

<form action="fallback.php" method="post" onSubmit="return
yourAjaxHandler();">

and having your handler return false does exactly what you want.

Regards, Tumi
Mar 19 '08 #4
Wed, 19 Mar 2008 17:57:52 +0200, /Tuomo Tanskanen/:
Yes, but doing it like

<form action="fallback.php" method="post" onSubmit="yourAjaxHandler();
return false;">

or even

<form action="fallback.php" method="post" onSubmit="return
yourAjaxHandler();">

and having your handler return false does exactly what you want.
The problem is I don't fully control the generated HTML code as it
is a JSF/RichFaces [1] application. The generated code looks
something like:

<form ...>
...
<input type="text" ... onchange="A4J.AJAX.Submit(...);" ... />
...
</form>

I've solved my problem adding a 'keydown' handler (I can do that):

<form ...>
...
<input type="text" ...
onkeydown="return preventDefault(event, this);"
onchange="A4J.AJAX.Submit(...);" ... />
...
</form>

Where the 'preventDefault' function definition is:

function preventDefault(evt, fld) {
if (!evt) {
evt = event;
}
if (evt.keyCode == 13) {
fld.blur();
fld.focus();
return false;
}
return true;
}

The blur-focus thing causes a 'change' event even in the browsers
which don't normally fire it when Enter gets pressed.

[1] http://labs.jboss.com/jbossrichfaces/

--
Stanimir
Mar 19 '08 #5
Wed, 19 Mar 2008 21:49:35 +0100, /Thomas 'PointedEars' Lahn/:
Stanimir Stamenkov wrote:
>I have a form without a submit button [...]

That is a Bad Thing, ...
(TM)... Unfortunately I'm not the one requiring the given thing and
I'm not in full control of the content as I'm using some application
framework which generates most of it.
><form name="form1" action=""
onsubmit="alert('submit ' + this.name);">

... especially as you use the `onsubmit' intrinsic event handler attribute
which requires a submit button.
I've used 'onsubmit' in my example only for testing purposes. In my
real page there's no such attribute. You may see my last reply to
Tuomo Tanskanen in this thread for my solution.

--
Stanimir
Mar 19 '08 #6
Thu, 20 Mar 2008 00:23:20 +0100, /Thomas 'PointedEars' Lahn/:
Stanimir Stamenkov wrote:
>I've used 'onsubmit' in my example only for testing purposes. In my
real page there's no such attribute. You may see my last reply to
Tuomo Tanskanen in this thread for my solution.

That only seems to be a solution. It will not work in 11% of user agents
at the minimum, and it will annoy the rest of the users. Needlessly. Can
your client afford to lose that many potential customers, the security issue
aside? I doubt it.
Could you elaborate more on what "will not work in 11% of user
agents" (and which ones)? What security issue do you see?

--
Stanimir
Mar 20 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: dogu | last post by:
Noob alert. Code is below. File is saved as a .php. What I'm trying to do: User uses 'select' box drop down list to pick a value. Value...
7
by: Shannon | last post by:
Hi everyone, I am trying to post some data to a form using the XMLHTTP object, and have run into a problem trying to find the proper location of...
19
by: Pete | last post by:
I have form/select which executes a function using onchange. No problem. However, when I validate the page with a strict HTML 4.01 doctype at...
5
by: Richard Cornford | last post by:
I am interested in hearing opinions on the semantic meaning of FORM (elements) in HTML. I have to start of apologising because this question...
2
by: anonieko | last post by:
Scenario: You have a page that is TOO slow to refresh. But it allows partial flushing of html contents. I.e. Submit button already appears but you...
10
by: Steve Last | last post by:
Hi all, I’m using IIS6 for our college Intranet and I’m having trouble using Request.Form. Here is my code: <% If...
16
by: whyyyy | last post by:
The script below works fine if the form is filled out and submitted. But a (blank) e-mail is sent whenever the page loads, even when the form is...
8
by: yawnmoth | last post by:
Say I have the following HTML: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> </head> <body>...
1
by: chromis | last post by:
Hi, I'm having trouble fully implementing the edit section of a contact admin system, so far I have written the following: - Bean (Contact.cfc)...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.