472,955 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 7720
"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
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
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
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
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
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
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
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
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
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
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.