newbie question - viewing form buttons submitted via 'post'?

M

mr. gone

hi, all. i'm still very new to the nuances of javascript and forms, so
please bear with me, and i apologize in advance if this sounds
profoundly stupid.

is it possible to view the values of html / javascript form buttons
(not 'submit' type - just vanilla input buttons) through the 'post'
method (say, in a php script)? i have a web page design where i'm
using buttons as 'toggles' between '+' and '-', and i need to be able
to read the values as part of the form submission. if this won't work,
i'll need to think up something else.

thanks in advance.
 
P

Pherdnut

hi, all. i'm still very new to the nuances of javascript and forms, so
please bear with me, and i apologize in advance if this sounds
profoundly stupid.

is it possible to view the values of html / javascript form buttons
(not 'submit' type - just vanilla input buttons) through the 'post'
method (say, in a php script)? i have a web page design where i'm
using buttons as 'toggles' between '+' and '-', and i need to be able
to read the values as part of the form submission. if this won't work,
i'll need to think up something else.

thanks in advance.

alert(document.forms[0][3].value);

In that snippet, the value attribute of the 4th element in the first
form on the page would be alerted. Look up the BOM for more details.
 
E

Evertjan.

Pherdnut wrote on 29 aug 2009 in comp.lang.javascript:
hi, all. i'm still very new to the nuances of javascript and forms, so
please bear with me, and i apologize in advance if this sounds
profoundly stupid.

is it possible to view the values of html / javascript form buttons
(not 'submit' type - just vanilla input buttons) through the 'post'
method (say, in a php script)? i have a web page design where i'm
using buttons as 'toggles' between '+' and '-', and i need to be able
to read the values as part of the form submission. if this won't work,
i'll need to think up something else.

thanks in advance.

alert(document.forms[0][3].value);

In that snippet, the value attribute of the 4th element in the first
form on the page would be alerted. Look up the BOM for more details.

It could be how you came to love the BOM,
and the vanilla taste of the OP,
but I would use the DOM:

alert(document.forms[0].elements[3].value);
 
E

Evertjan.

Stefan Weiss wrote on 29 aug 2009 in comp.lang.javascript:
To paraphrase: will the names/values of
button-type input fields be submitted with the rest of the form?

What gives a lifeform-as-we-know-it headache?

<form name='aLifeForm'>
<input type='submit' name='person' value='John'><br>
<input type='submit' name='person' value='Pierre'><br>
<input type='submit' name='person' value='Strangelove'><br>
</form>

Answer: some IE forms do not perform correctly, IE8 and other browser
lifeforms seem to do, but don't bet on it.

<form onsubmit='return f(this)' name='aLifeForm'>
<input type='submit' name='person' value='John'><br>
<input type='submit' name='person' value='Pierre'><br>
<input type='submit' name='person' value='Strangelove'><br>
</form>
<script type='text/javascript'>
function f(x){
alert(x.elements['person'].value);
return false;
};
</script>

Answer: some browser lifeforms do perform correctly, some not.


<form name='aLifeForm'>
<input name='aValue' value='3'><br>
<input type='submit' value='Submission'
onclick='return f(this)'><br>
</form>
<script type='text/javascript'>
function f(x){
alert(x.form.elements['aValue'].value);
return false;
};
</script>

Answer: some browser lifeforms can't even find the parent form of a
submissive lifeform button, they are factual orphans.

Because of Javascript crashing, the form however is submitted,
where the return false would have you expect otherwise.
A crossbrowser debugging nightmare.

Not crossbrowser tested this time!
 
E

Evertjan.

bill wrote on 29 aug 2009 in comp.lang.javascript:
In my experience,
<input type="button" value="Yada" name="yadabutton"> will return
"yadabutton" for the value of 'Yada' in the POST array.
bill

You experience is limited, Bill.

Try this in IE6 or IE7 with multiple buttons with the same name and
different values.
 
M

mr. gone

Pherdnut wrote on 29 aug 2009 in comp.lang.javascript:
is it possible to view the values of html / javascript form buttons
(not 'submit' type - just vanilla input buttons) through the 'post'
method (say, in a php script)? i have a web page design where i'm
using buttons as 'toggles' between '+' and '-', and i need to be able
to read the values as part of the form submission. if this won't work,
i'll need to think up something else.
alert(document.forms[0][3].value);
In that snippet, the value attribute of the 4th element in the first
form on the page would be alerted. Look up the BOM for more details.
It could be how you came to love the BOM,
and the vanilla taste of the OP,

Ah, the famous doomsday script...
"Mein Führer, I can walk!"
but I would use the DOM:
alert(document.forms[0].elements[3].value);

Right. Doesn't answer the OP's question, but to be fair, it wasn't
really a JS related question. To paraphrase: will the names/values of
button-type input fields be submitted with the rest of the form? Tough
question. I wonder if there's a way to find an answer, like trying it
out, reading the specs, or googling it... Probably not, so I'll just
answer: no, OP, they won't, and yes, you'll have to think up something
else. You could, for example, add a hidden input field for each +/-
button, and use JS to update its value when the corresponding button is
clicked. Or you could use a single hidden field and serialize the +/-
states in its value. Or you could use Ajax to save the +/- states every
time a button is clicked. Or you could dynamically add hidden fields
only for the parts where + is active (whatever that means in your case).
Or you could disable the form fields in the - sections and let the
server side figure out what's active and what's not. There's a hundred
more ways to get your values to the server, but without any concrete
information that's all I can offer.

Anyway, I enjoyed the Strangelove reference. Got to rent that movie
again one of these days.

cheers,
stefan

okay, thanks for the answer. i was hoping 'input'-type buttons would
work, but from what you're saying, they won't.
 
E

Evertjan.

mr. gone wrote on 29 aug 2009 in comp.lang.javascript:

[please do not quote signatures on usenet]
okay, thanks for the answer. i was hoping 'input'-type buttons would
work, but from what you're saying, they won't.

That is not a good attitude for a programmer.

Why don't you check it out for yourself?

Jus make them work,
you just have to check your code on all relevant browsers.
 
T

Thomas 'PointedEars' Lahn

Pherdnut said:
alert(document.forms[0][3].value);

Should be

window.alert(document.forms[0].elements[3].value);
In that snippet, the value attribute of the 4th element in the first
form on the page would be alerted. Look up the BOM for more details.
^^^
Did you mean "_DOM API documentation_"?


PointedEars
 
P

Pherdnut

First of all, the forms object is a part of the BOM. There is no such
thing as document.divs[3] for instance. So the first correction used
the BOM and then went long with additional stuff from the DOM.

And by BOM I mean Browser Object Model. Old-school. Works just fine
and I find I prefer it for dealing with forms for some reason. I think
because form elements are so incredibly weakly spec'd that I prefer to
think of them as junk that comes from the OS (which they typically
are) rather than things that behave like proper DOM objects whose
presentation can be fully controlled through CSS properties. I'm not
sure they got updated for IDs but name attributes, which I avoid, can
be used to call the form elements by name.
 
T

Thomas 'PointedEars' Lahn

Pherdnut said:
First of all, the forms object is a part of the BOM.

No, `forms' is the name of a property of the DOM host object referred to by
`document', which is defined by the respective language-independent DOM
(Document Object Model) API. In particular, nowadays this object or its
prototype implement the HTMLDocument interface of W3C DOM Level 2 HTML which
defines a `forms' attribute (in the IDL, not to be confused with a markup
attribute) for which the implementation SHOULD implement the HTMLCollection
interface (at least four widely distributed implementations actually do that).

There is no such thing as document.divs[3] for instance.

That depends on the runtime environment, in particular on the layout engine
and its DOM implementation, and on the document that is represented by the
current view's document tree. But your statement is actually unrelated to
your earlier assertion anyway, thus cannot provide the proof for the latter.

In fact, for historical reasons dating back to Netscape Navigator 2.0 and
its JavaScript 1.0 implementation, there *is* a document.divs[3] property in
many DOM implementations (AFAIK including that of MSHTML, Gecko, Apple
WebKit, KHTML, Opera, and Netscape 4.x) if `divs' is the value of a HTML
`form' element's `name' attribute and that element contains at least four
form controls (e.g., `input' elements). That property would hold a
true-value, a reference to the DOM object representing the fourth control
within `<form ...>...</form>' in the source code.
(document.forms["divs"].elements[3] would the be proper, less error-prone,
backwards-compatible and standards-compliant referencing, of course.)

Another possibility to achieve this would be a successful, though naturally
error-prone, write access to augment the host object referred to by
`document' with a user-defined property named `divs', and this property
referring to an object having a property named `3':

document.divs = {"3": 42};
window.alert(document.divs[3]);

Or please consider this: By slight modification of your example, there is a
true-value document.all.divs[3] property in all MSHTML-based browsers, in an
HTML document that has at least four elements with `divs' for the value of
their `name' attribute, respectively. But if the layout engine used is not
MSHTML, or the document type is not (recognized as) HTML, there is no
guarantee that this is going to work. So there is _not_ (only) a (single)
"BOM". (A test case is readily available: Go to

<http://www.google.com/preferences>

and enter

javascript:window.alert(document.all.lr[3].value);

in the address/location bar after the former has been fully loaded. If I'm
not mistaken, the alert message box should say `lang_be', for Belarusian,
everywhere where this is supported, and throw a ReferenceError(-like)
exception otherwise. It should also be supported in Firefox 3.5.1 [I tested
positive with Iceweasel], with a console warning, only because the document
is rendered in Quirks Mode due to the missing DOCTYPE declaration; other,
especially older, versions of Firefox would differ as compatibility with
MSHTML did not have that high a priority back then.)
So the first correction used the BOM and then went long with
additional stuff from the DOM.
Nonsense.

And by BOM I mean Browser Object Model.

Whereas the DOM that it is defined by instead, is both an application- and
language-inpendent API. The used layout engine, the (recognized) document
type, and the used rendering mode matter instead.
Old-school.

I'm afraid you don't know what you are talking about, probably being
considerably mislead by authors of books or libraries not/no longer to be
recommended.
Works just fine

Provably, and proved here (also numerous times before), it doesn't.
Instead, it has turned out to be necessary to distinguish between the
specified programming language, ECMAScript, and its Editions; the
implementations of that language (e.g. JavaScript, JScript) and their
versions; the application-independent, language-independent, layout-engine-
and layout-mode-dependent DOM (which nowadays implements, for example,
several interfaces of the W3C DOM API); the public browser API (that I have
come to generalize under the term AOM, Application Object Model; which
provides, for example, the (still-proprietary) host-defined `window'
property of the ECMAScript Global Object); and external APIs, whereas each
API provides an ECMAScript binding. In fact, this is an important
distinction that especially many book and library authors have yet to fully
understand, and to make in their books and libraries.

See also said:
and I find I prefer it for dealing with forms for some reason.

"Some reason" is hardly sufficient a reason. A "belly feeling" about a
technology or a technique alone does not exactly promote understanding of
it, as you should see by now, and if not yet, after having read below.
I think because form elements are so incredibly weakly spec'd

Such as in the W3C HTML 4.01 Specification or the W3C DOM Level 2 HTML
Specification? I shall assume, in your favor, that to have ended up with
this assessment you have been relying on Flanagan, Goodman, Resig, all of
them, or the like, for far too long.

<http://www.w3.org/TR/html401/interact/forms.html#h-17.3>
<http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-40002357>
that I prefer to think of them as junk that comes from the OS

The OS does not even enter into any of this.
(which they typically are)

That MSHTML uses "windowed controls" (objects that have a window handle,
HWND) from the Win32 API for rendering some elements, especially form
controls (with the known disadvantages of that), is no indication at all
that (X)HTML `form' *elements* would "typically" have to do with the OS.
To begin with, "windowed controls" are never used for rendering (X)HTML
`form' *elements* by MSHTML.
rather than things that behave like proper DOM objects whose
presentation can be fully controlled through CSS properties. I'm not
sure they got updated for IDs but name attributes, which I avoid, can
be used to call the form elements by name.

That is gibberish. Please get yourself informed before further (mis-)parroting.

<http://jibbering.com/faq/>


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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top