order of form elements

G

groups2

I have a form with a dropdown (select) menu a text input field and
some hidden values, along with an input botton that triggers an ajax
function that submits the form.
If the button is after the select statement, the chosen value of the
selection of the dropdown is posted. But if the button is before the
select statement, everything in the form is posted but the value of
the selection

Why would the select box be left out of the post ?
 
T

Thomas 'PointedEars' Lahn

I have a form with a dropdown (select) menu a text input field and
some hidden values, along with an input botton that triggers an ajax
function that submits the form.

It would have been wise to post that function.
If the button is after the select statement,

There are no statements in the (eXtensible) HyperText *Markup Language*.
You mean a `select' _element_.
the chosen value of the selection of the dropdown is posted. But if
the button is before the select statement, everything in the form is
posted but the value of the selection

Why would the select box be left out of the post ?

As you have not provided enough information, I have to make an educated
guess: your "ajax function" iterates through the form elements collection
(where the numeric index of an element is defined by the arrangement of
the controls in the markup) until it finds the submit button, i.e. an
element of type "submit". That algorithm should be modified.


PointedEars
 
G

groups2

No, but close. I knew where the form iteration occured and when I
looked at it it was like this:

var numberElements = frm.elements.length-1;
for(var i = 0; i < numberElements; i++) {
queryString+=frm.elements.name
+"="+encodeURIComponent(frm.elements.value);
if(i < numberElements-1) queryString+="&";
}

So for some reason, it was written to always skip the last element. I
took out the -1 from "length-1" and it works as expected now. Thanks,
you really helped.
 
R

RobG


Please don't top post, reply below trimmed quotes.

but close. I knew where the form iteration occured and when I
looked at it it was like this:

var numberElements = frm.elements.length-1;
for(var i = 0; i < numberElements; i++) {
queryString+=frm.elements.name
+"="+encodeURIComponent(frm.elements.value);
if(i < numberElements-1) queryString+="&";
}

So for some reason, it was written to always skip the last element. I
took out the -1 from "length-1" and it works as expected now. Thanks,
you really helped.


You should also only return controls that have a value for the name
attribute, and do not expect select elements to return their value in
all browsers (though likely those that don't aren't supported by your
"AJAX" form anyway).

You might also consider using an array to join the value/name pairs
with '&', something like:

function ... ()
{
var element, queryString = [];
var numberElements = frm.elements.length;

for(var i=0; i<numberElements; i++) {
element = frm.elements;
if (element.name && element.name != '') {
queryString.push(element.name + "="
+ encodeURIComponent(element.value));
}
}
return queryString.join('&'); }
}
 

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
474,157
Messages
2,570,879
Members
47,413
Latest member
KeiraLight

Latest Threads

Top