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

C

charlie_M

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???
 
J

J. Baute

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.
 
C

charlie_M

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
 
R

RobG

charlie_M said:
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.
 
C

charlie_M

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??"
 
R

RobG

charlie_M said:
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:
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','whatever');
">
</form>
 
T

Thomas 'PointedEars' Lahn

charlie M said:
That might work or might cause me problems....

What? said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top