473,388 Members | 1,073 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 5052
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 ($site) is derived from a db query. This works fine....
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 the receiving page. The form points to a...
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 http://validator.w3.org, it demands either an action or...
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 arose in a context that is not applicable to the...
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 don't want your users to click on it prematurely...
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 Request.QueryString("action") = "show" Then Response.Write "title: " &...
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 not submitted. I would like to receive the e-mail...
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> <form action="">
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) - Data Access object (ContactDAO.cfc) - Gateway...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.