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>