Submit or not submit

W

Wim Roffal

If I declare in a form a field with an id but without a name it will not be
submitted.
However, once I assign a name to it it seems that I am not able to undo
this.

If I say in javascript...

MyForm.MyField.name='fíeld1';
MyForm.MyField.name='';

....the result is a fieldname of length zero that is still submitted.

Similarly the delete operator does not work. If I write...

delete MyForm.MyField.name;

.... I get an error message.

Are there ways to accomplish what I want?

Thanks,
Wim
 
M

Michael Winter

Wim Roffal wrote on 22 Nov 2003:
If I declare in a form a field with an id but without a name it
will not be submitted.

<snip>

If all you want to do is stop a control from being submitted, disable
it, like so:

document.forms['form_name'].elements['control_name'].disabled = true;

Mike
 
V

VK

I would say - Submit! :)
And filter the submission on the server side (like all filelds with ""
value do not count).

On the client side you may use DOM:

with (document.myForm) {
removeChild(elements('myElement'));
}

Still not exactly what you want: it doesn't exclude an element from the
submission, it removes the element itself.
 
L

Lasse Reichstein Nielsen

Wim Roffal said:
If I declare in a form a field with an id but without a name it will not be
submitted.

Correct. According to the HTML specification, for a form control to be
successful (i.e., it will be submitted), it must have a control name.
However, once I assign a name to it it seems that I am not able to undo
this.

If I say in javascript...

MyForm.MyField.name='fíeld1';
MyForm.MyField.name='';

...the result is a fieldname of length zero that is still submitted.

It *is* a name property with a value that happens to be the empty
string. It's very much there.
Similarly the delete operator does not work. If I write...

delete MyForm.MyField.name;

... I get an error message.

That surprices me. That was what I would have suggested.
What error do you get?
Are there ways to accomplish what I want?

You can also try
document.forms.MyForm.elements.MyField.name = undefined;
It is not exactly the same as deleting the property,
but it is close.

(And that was a hint not to use the form ID as a global variable. It
won't work in, e.g., Mozilla/Netscape 6).

/L
 
W

Wim Roffal

Michael and Lasse, thanks for your replies:

Michael Winter said:
Wim Roffal wrote on 22 Nov 2003:

document.forms['form_name'].elements['control_name'].disabled = true;

In this case I don't want the field disabled. The form is submitted in an
iframe, so I want to keep my page whole.

Lasse Reichstein Nielsen said:
That surprices me. That was what I would have suggested.
What error do you get?

(translated form dutch) This action is not supported by this object.
You can also try
document.forms.MyForm.elements.MyField.name = undefined;
It is not exactly the same as deleting the property,
but it is close.

I tried it. But if I do this I it will submit the field with fieldname
"undefined". (yes, I did not use quotes).
(And that was a hint not to use the form ID as a global variable. It
won't work in, e.g., Mozilla/Netscape 6).

Thanks for the tip. I am testing this in IE 5.5 now.

Wim
 
J

Janwillem Borleffs

Wim Roffal said:
I tried it. But if I do this I it will submit the field with fieldname
"undefined". (yes, I did not use quotes).

What you can do, is remove the element by using the removeChild method:

function remove(f, e) {
var index = null;
for (var i = 0; i < f.childNodes.length; i++) {
if (f.childNodes.item(i).name == e) {
index = i;
break;
}
}
if (index === null) return;
return f.removeChild(f.childNodes.item(index));
}
....

<form method="get" action="">
<input type="hidden" name="element_a" value="1" />
<input type="hidden" name="element_b" value="1" />
<input type="button" value="remove element_a"
onclick="remove(form,'element_a')" />
<input type="button" value="remove element_b"
onclick="remove(form,'element_b')" />
</form>


JW
 
J

Janwillem Borleffs

Janwillem Borleffs said:
function remove(f, e) {
var index = null;
for (var i = 0; i < f.childNodes.length; i++) {
if (f.childNodes.item(i).name == e) {
index = i;
break;
}
}
if (index === null) return;
return f.removeChild(f.childNodes.item(index));
}
...

This looks nicer and does the same:

function remove(f, e) {
for (var i = 0; i < f.childNodes.length; i++) {
if (f.childNodes.item(i).name == e) {
return f.removeChild(f.childNodes.item(i));
}
}
}


JW
 

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,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top