Connecting Tech Pros Worldwide Help | Site Map

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

  #1  
Old July 23rd, 2005, 08:52 PM
charlie_M
Guest
 
Posts: 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???

  #2  
Old July 23rd, 2005, 08:52 PM
J. Baute
Guest
 
Posts: n/a

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


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.

  #3  
Old July 23rd, 2005, 08:52 PM
charlie_M
Guest
 
Posts: n/a

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


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

  #4  
Old July 23rd, 2005, 08:53 PM
RobG
Guest
 
Posts: n/a

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


charlie_M wrote:[color=blue]
> 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
>[/color]

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
  #5  
Old July 23rd, 2005, 08:53 PM
charlie_M
Guest
 
Posts: n/a

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


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

  #6  
Old July 23rd, 2005, 08:54 PM
RobG
Guest
 
Posts: n/a

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


charlie_M wrote:[color=blue]
> That might work or might cause me problems.... but I would much rather
> have the question answered....[/color]

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:
[color=blue]
> EXAMPLE:
>[/color]
onClick="document.forms[0].MYBUTTON.value='SIMPLE';document.forms[0].submit()"

Ok, you set the value of an existing field, great.
[color=blue]
> In my CGI I see "MYBUTTON" = "SIMPLE"
>
> and this works fine.... except that the element MYBUTTON must exist as
> a hidden field.[/color]

It doesn't need to be hidden, it can be any form element that has a
value attribute and is successful.
[color=blue]
> 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.[/color]

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.
[color=blue]
> How can I INSERT a (non-existing) field and value on this example line
> of code???[/color]

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).
[color=blue]
>
> "how can I add a nonexistant field and value at submit() time??"
>[/color]

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
  #7  
Old July 23rd, 2005, 08:54 PM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a

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


"charlie M" schrieb:
[color=blue]
> That might work or might cause me problems....[/color]

What? <http://www.jibbering.com/faq/faq_notes/pots1.html>
[color=blue]
> but I would much rather have the question answered....
>
> "how can I add a nonexistant field and value at submit() time??"[/color]

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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Dynamically Add and Remove Accordion Control Panes Allen Chen [MSFT] answers 3 November 6th, 2008 02:45 PM
python-dev Summary for 2006-08-16 through 2006-08-31 steven.bethard@gmail.com answers 0 October 18th, 2006 07:05 PM
Can't make this page work scottyman@comcast.net answers 6 March 9th, 2006 04:25 AM
Script to Add and Count Values Yellowbird answers 5 July 20th, 2005 01:57 PM
Reality Check: Session Hijacking mrbog answers 27 July 17th, 2005 06:13 AM