By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,896 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

How can I add a 'non-existant' field and value at submit() time???

P: n/a
I figured out via various help from this forum...

EXAMPLE:
onClick="document.forms[0].MYBUTTON.value='SIMPLE';document.forms[0].submit()"

In my CGI I see "MYBUTTON" = "SIMPLE"

and this works fine.... except that the element MYBUTTON must exist as
a hidden field.

The problem with this solution is when I have another type of input
element on the same form with a "onchange=submit()" and the user has
BACKed into the page.... the hidden field holds the previous value of
MYBUTTON. It is, in this instance, 'fixable' by simply clearing the
value (pre-submit) on the second element.. but it complicates the
issue.

How can I INSERT a (non-existing) field and value on this example line
of code???

Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
There might be a more straightforward way to solve your problem.

I'm guessing that you have a number of submit buttons on your page
where you set the hidden fields value, and then submit your form.

Why not use a visible radio button control instead to allow the user to
select the type of document he/she wants, and use only a single submit
button to submit your form.

That way the user will be able to visually verify the type of chosen
document when he uses the back-button, which solves your issue.

A single submit button also makes more sense imho.

regards,
J.

Jul 23 '05 #2

P: n/a
No Mr Baute... there is not a "simple-er" method to solve this. It is
much more of a complex issue than anyone could assume. The page has in
excess of 12 "submit" type buttons.. not any of them are images or, in
fact, buttons. The pages are written in multiple computer languages.
The submit job is allocated to javascript.. which is why I asked the
question here.

Is the answer to the question.. "it cannot be done!"???

TIA
Chuck

Jul 23 '05 #3

P: n/a
charlie_M wrote:
No Mr Baute... there is not a "simple-er" method to solve this. It is
much more of a complex issue than anyone could assume. The page has in
excess of 12 "submit" type buttons.. not any of them are images or, in
fact, buttons. The pages are written in multiple computer languages.
The submit job is allocated to javascript.. which is why I asked the
question here.

Is the answer to the question.. "it cannot be done!"???

TIA
Chuck


I guess you want something to happen when the user navigates to your
page with the 'back' button. Add an onload function that does whatever
- clear the field, set it to a default, go crazy.
--
Rob
Jul 23 '05 #4

P: n/a
That might work or might cause me problems.... but I would much rather
have the question answered....

"how can I add a nonexistant field and value at submit() time??"

Jul 23 '05 #5

P: n/a
charlie_M wrote:
That might work or might cause me problems.... but I would much rather
have the question answered....
I have attempted to answer your question - perhaps you haven't been
asking it clearly? You have provided no script or even HTML to help
explain.

Let's look at the original post:
EXAMPLE:
onClick="document.forms[0].MYBUTTON.value='SIMPLE';document.forms[0].submit()"

Ok, you set the value of an existing field, great.
In my CGI I see "MYBUTTON" = "SIMPLE"

and this works fine.... except that the element MYBUTTON must exist as
a hidden field.
It doesn't need to be hidden, it can be any form element that has a
value attribute and is successful.
The problem with this solution is when I have another type of input
element on the same form with a "onchange=submit()" and the user has
BACKed into the page.... the hidden field holds the previous value of
MYBUTTON. It is, in this instance, 'fixable' by simply clearing the
value (pre-submit) on the second element.. but it complicates the
issue.
If the user "backs" into the page (I presume you mean uses the back
button or equivalent) then the value of the form fields is whatever the
browser decides. Some will clear all values, some will restore it to
whatever it was last time.

Submitting the form using 'onchange' is plain dumb, if the user backs
into the page and it's already how they want it, they have to change
something (and then click elsewhere in the form most likely) for the
form to submit.

As soon as they change something, the form submits with the wrong
values.

In any event, your current issue is to set the value of a hidden field
when an event occurs. You want to make that simpler by not only
changing the value, but adding the element to the form also. I can't
see how that is less complicated.
How can I INSERT a (non-existing) field and value on this example line
of code???
Below is a script that adds an element to a form. You can add the code
to the onclick in the source HTML (so you'd have to add it to all your
12 or so submit buttons) or add it as a script then call it when the
button is clicked (depends how much code you want to write).

"how can I add a nonexistant field and value at submit() time??"


I've added it as an onclick to a button that also submits the form.

The form will only submit if the user has JavaScript available. You
could make the button a submit button, or run the function using the
form's onsubmit event, but then if the user has JavaScript disabled or
not available, the form will submit without your extra field. Over to
you.
<script type="text/javascript">
function addField(f,n,v){
var oText = document.createElement('input');
oText.type = 'hidden';
oText.name = n;
oText.value = v;
f.appendChild(oText);
f.submit(); // Not needed if called from a submit button
} // or onsubmit event
</script>

<form action="">
<input type="text" name="blah" value="blah-blah">
<input type="button" value="blah" onclick="
addField(this.form,'nonexistantTextField','whateve r');
">
</form>

--
Rob
Jul 23 '05 #6

P: n/a
"charlie M" schrieb:
That might work or might cause me problems....
What? <http://www.jibbering.com/faq/faq_notes/pots1.html>
but I would much rather have the question answered....

"how can I add a nonexistant field and value at submit() time??"


Your Question Mark key is borken. Try

<form action="..." ...>
...
<script type="text/javascript">
function addField(f, sName, sValue)
{
var t;
if (f && sName
&& typeof document != "undefined"
&& ((t = typeof document.createElement) == "function"
|| (t == "object" && document.createElement))
&& ((t = typeof f.appendChild) == "function"
|| (t == "object" && f.appendChild)))
{
var oField = document.createElement('input');
if (oField)
{
oField.type = "hidden";
oField.name = sName;
oField.value = sValue || "";
f.appendChild(oField);
}
}

}
</script>
<input type="submit" onclick="addField(this.form, 'foo', 'bar');"
...
</form>

Whether that works, i.e. if that added control value is submitted, in
a user agent is a different matter. Required are enabled support for
client-side JS and support of W3C DOM Level 2 HTML. Double-checking
the submitted value server-side is a must.
PointedEars
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.