473,503 Members | 11,783 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need to have an <a> tag submit a form (cross platform solution needed)

Hi all.

I have a form, and several text and image links on it that should
submit the form with different actions.

I prepared a simple page with just the code that's not working.

PROBLEM: The form won't submit if the link is clicked, but will submit
if the SUBMIT button is clicked. I need to call a function to change
the form's action according to user's input before it is submitted.

Thanks in advance for any help.

The code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Submit test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />

<script language="javascript" type="text/javascript">
function SendForm() {
d = eval('document.forms[0]');
d.action = 'http://localhost:8080/test.php';
d.testVar.value = 'new value';
alert(d.testVar.value);
d.submit();
}
</script>
</head>

<body>
<form name="form1" method="post">
<a href="#" onclick="SendForm(); return true;">Submit</a>
<input type="submit" name="submit" value=" submit ">
<input type="hidden" name="testVar" value="original value">
</form>

<?php
// Debug stuff START --------------------------------
// Echo time, to check if the form has been submitted
echo time() . '<br />';

// See what's inside POST
var_export($_POST);
// Debug stuff END ----------------------------------
?>

</body>
</html>
Jul 20 '05 #1
4 7767
"Sarah" <sa***@nospam.com> wrote in message
news:pd********************************@4ax.com...
<snip>
function SendForm() {
d = eval('document.forms[0]');
Using eval as a property accessor is always wrong (and you are also
quite unlikely to need to use eval for anything else) but you have
compounded the error by passing the eval function a string literal. It
will interpret 'document.forms[0]' in exactly the same way as it would
interpret the same code written directly in the source:-

d = document.forms[0];

-so there is little point in using eval here (even if it wasn't
unnecessary anyway).

<URL: http://jibbering.com/faq/#FAQ4_40 >
<URL: http://jibbering.com/faq/#FAQ4_39 >

The variable - d - is not declared as local to this function using the -
var - keyword and as a result will become a global variable.
Unnecessarily creating global variables is a common source of coding
errors due to unexpected interactions (often difficult to debut).
Generally, do not give variables more scope than they need. Which means
variables within functions should always be local unless there is a good
reason for them to be global.

<snip><form name="form1" method="post">
<a href="#" onclick="SendForm(); return true;">Submit</a>
The onclick handler should return false (not true) in order to cancel
the navigation specified in the HREF (the "#", which is normally
interpreted as navigation to the top of the current page).
<input type="submit" name="submit" value=" submit ">

<snip>

A common error in HTML forms is to give the submit button the name
"submit" (or reset buttons the name "reset"). Form element objects make
named form elements available as named properties of the form (mostly),
so a submit button named "submit" is referred to by a property of the
form object with the name "submit". However, the form object starts off
with a property called "submit", the submit method that you are
attempting to call. And the browsers response to finding a submit button
with the name "submit" is to replace the reference to the submit method
held under the name "submit" with a reference to the submit button.
Preventing JavaScript from submitting the form.

Generally, it is a bad idea to make form submission dependent on
JavaScript. Server side solutions are much better and HTML forms are
very reliable on their own.

Richard.
Jul 20 '05 #2
On Tue, 25 Nov 2003 02:36:50 -0300, Sarah wrote:
Hi all.

I have a form, and several text and image links on it that should submit
the form with different actions.

I prepared a simple page with just the code that's not working.

PROBLEM: The form won't submit if the link is clicked, but will submit
if the SUBMIT button is clicked. I need to call a function to change the
form's action according to user's input before it is submitted.

Thanks in advance for any help.

The code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html
xmlns="http://www.w3.org/1999/xhtml"> <head>
<title>Submit test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>

<script language="javascript" type="text/javascript"> function
SendForm() {
d = eval('document.forms[0]');
d.action = 'http://localhost:8080/test.php'; d.testVar.value = 'new
value';
alert(d.testVar.value);
d.submit();
}
</script>
</head>
Try

<body onSubmit="return SendForm()">

to make the submit button use the SendForm() routine
<form name="form1" method="post">
<a href="#" onclick="SendForm(); return true;">Submit</a>
and

<a href="javascript:SendForm(); return true;">Submit</a>

to make the link execute the SendForm() routine.

<input type="submit" name="submit" value=" submit "> <input type="hidden" name="testVar" value="original value">
</form>


--,
MVH
DaveG
Jul 20 '05 #3
Dave Griffiths wrote on 25 Nov 2003:
On Tue, 25 Nov 2003 02:36:50 -0300, Sarah wrote:
<snip>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <head>
<title>Submit test</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
You are using intrinsic events, but you are not specifying a default
language. That produces invalid HTML. Include the following entity in
your HEAD block:

<META http-equiv="Content-Script-Type" content="text/javascript">

A similar line is needed if you use style attributes. Read the 'Style
Sheets' section of the HTML 4.01 specification for more information.
<script language="javascript" type="text/javascript">
The language attribute is not required: type is mandatory and always
sufficient.
function SendForm() {
d = eval('document.forms[0]');
As Mr Cornford already stated, that line does two things:

1) 'd' is created as a global variable. You don't want that.
2) Uses eval() to perform the exact same thing as would happen if it
were not present at all.

Change it to:

var d = document.forms[0];

You could also make further changes. Identifiers should always be
meaningful - 'd' is not. It is also a good idea to use the form name
rather than the index. It reduces maintenance effort.

var d = document.forms['form_name'];
d.action = 'http://localhost:8080/test.php';
Why are you specifying the action attribute here, and not in the FORM
element? A FORM element is not valid if no action has been specified.
d.testVar.value = 'new value';
It's better to use:

d.elements['testVar'].value = '...';

That will work across more browsers.

<snip>
<body onSubmit="return SendForm()">
The HTML 4.01 specification does not specify a onsubmit intrinsic
event for anything other than the FORM element. That line above,
irrespective of whether it works or not, is incorrect.
to make the submit button use the SendForm() routine
To make a button use a particular function, use this:

<BUTTON type="submit" onclick="SendForm(); return false"Submit</BUTTON> <form name="form1" method="post">
As I said earlier, a FORM element *requires* an action attribute, and
'form1' should be changed to something more meaningful.
<a href="#" onclick="SendForm(); return true;">Submit</a>


Returning true, or not specifying return at all, results in the
browser navigating to the URI specified in the href attribute. You
should return false to stop it performing that action.
and

<a href="javascript:SendForm(); return true;">Submit</a>
You should *not* use the JavaScript protocol specifier
('javascript:') inside the href attribute. Intrinsic events remove
that need. Moreover, if not recognised, the JavaScript protocol
results in a malformed URI.
to make the link execute the SendForm() routine.

<input type="submit" name="submit" value=" submit ">


It is better to use the BUTTON element for push (standard), submit,
and reset button types. You should not use the name 'submit' or
'reset' for any controls as this interferes with the JavaScript
methods of that name.

<snip>

My comments use HTML as I do not know XHTML. However, as XHTML merely
extends HTML, they are valid, all the same.

Mike

--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #4
Thank you all very much for taking the time to reply (and quite a fast
response I got!).

Your answers have encouraged me to take the time and re-code the whole
page with more meaningful names, controlled variable scopes, and
especially a better designed page that uses a more standard interface
with submit buttons instead of <a> links.

Again, one thousand "thanks" to members of this group.

Cheers,
Sarah.
Jul 20 '05 #5

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

Similar topics

10
7044
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
3
12895
by: Ben | last post by:
Here's my form: <form name="aForm" method='post'> <input type=file name=file1 onkeypress='KeyPress()'><br> <a id='attachMoreLink' href='javascript:AddFileInput()">Attach More Files </a> <input...
4
3703
by: rob c | last post by:
This is a minor thing and only appears in IE (so far), but I'd like to know to correct it (if possible). Whenever I use a form on a webpage, Explorer always leaves a blank line following the...
1
7673
by: Robert Dodier | last post by:
Hello, Sorry for asking what must be a FAQ, but I wasn't able to find the answer. I have an XML document fragment which I want to store as a text string. I want a function to convert any XML...
8
1325
by: Benjamin Bécar | last post by:
Hello everyone. I have to find a correct architecture to achieve this XML <=Text conversion platform. The platform (based on Win2003Server) will have to deal with 21 million XML files and 16...
10
2947
by: Szabolcs Horvát | last post by:
Consider the attached example program: an object of type 'A' is inserted into a 'map<int, Am;'. Why does 'm;' call the copy constructor of 'A' twice in addition to a constructor call? The...
1
3643
by: Dancefire | last post by:
Hi, everyone, I'm trying to use std::codecvt<to do the encoding conversion. I am using following code for encoding conversion between wchar_t string and char string(MBCS). I am not sure am I...
14
3116
by: Michael | last post by:
Since the include function is called from within a PHP script, why does the included file have to identify itself as a PHP again by enclosing its code in <?php... <?> One would assume that the...
3
2928
by: newbie | last post by:
Same thing g++ complains when using hash_map<>, but is happy with map<--I understand hahs_map is not standardized, but since the compiler didn't complain something like 'hash_map<not defined', I...
10
14128
by: neverquit | last post by:
hi , Iam Nagesh,Begineer in using Ajax,well i have been using ajax in application, i have faced a problem while placing the responseTEXT into the <div> tag positioned inside the <form> tag iam...
0
7194
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
7070
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.