PDA

View Full Version : change form.action while having another element named "action"



gnznroses
10-26-2007, 09:56 PM
i need to change the action of my form like so:
document.regform.action = 'preregister.php';

the problem is, i have to have this also:
<input type="hidden" name="action" value="createnew">

it works fine in Firefox, but in IE, i get an error that the object doesn't support that method. i guess it's trying to change the hidden input (code works fine without that input).

surely there's a way to do this?
i've tried other means of changing the action, like:
document.getElementById('blah').action
or
document.forms[0].action

but same result with all.

Twey
10-27-2007, 09:32 AM
I think you may have to resort to document.forms.regform.setAttribute('action', 'preregister.php'). The easiest solution, though, is of course to rename the "action" element.

jscheuer1
10-27-2007, 01:43 PM
That seems reasonable, Twey. In testing here, it didn't pan out though. This little demo did:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function chng_a(el){
for (var i = 0, a=[], p=el.form.elements; i < p.length; i++)
if(p[i].name&&p[i].name.toLowerCase()=='action')
a.push(p[i].parentNode.removeChild(p[i]));
el.form.action='http://www.google.com';
for (var i = 0; i < a.length; i++)
el.form.appendChild(a[i]);
}
</script>
</head>
<body>
<form action="#" name="regform">
<div><input type="hidden" name="action" value="createnew">
<input type="button" onclick="chng_a(this);" value="Change Action"><br>
<input type="submit" value="Go!"></div>
</form>
</body>
</html>

Twey
10-27-2007, 05:55 PM
However, that a) is a Horribly Ugly Hackę, and b) rearranges the form elements, something that's likely to have a less-than-pleasant effect on presentation unless every element is absolutely positioned (and if they all are, you've worse problems to worry about).
In testing here, it didn't pan out though.You've been here long enough to know that this isn't very helpful in diagnosing a problem. Can you say what went wrong?

jscheuer1
10-27-2007, 08:29 PM
However, that a) is a Horribly Ugly Hackę, and b) rearranges the form elements, something that's likely to have a less-than-pleasant effect on presentation unless every element is absolutely positioned (and if they all are, you've worse problems to worry about).

Good point about the rearrangement, but with what was put forward (a hidden element), it should be no problem as long as the form's elements aren't later referenced by number, something easy to avoid. However, if the layout must be preserved, the array could become multidimensional and store a reference to the element's position, so that it could be reinserted there.

I'm not sure how it is a hack though.


You've been here long enough to know that this isn't very helpful in diagnosing a problem. Can you say what went wrong?

I thought that should have been obvious. To wit, the setAttribute method, although specifically designed to seek out an attribute and not an element, nevertheless found only the element in IE 7 (and in Opera 9.01).

As a side note, I tried (and discarded):


for (var i in el.form)
if(i=='action')
el.form[i]='new_url';

Because only IE saw two i=='action' members of the form, but even so, when it went to access either of them, accessed only the element. Opera and FF saw and accessed only one, the element.

Twey
10-27-2007, 08:43 PM
I'm not sure how it is a hack though.Removing every element with a name of "action", accessing the property, then putting them all back in?
1. n. Originally, a quick job that produces what is needed, but not well.

jscheuer1
10-27-2007, 08:53 PM
There is only one.

Twey
10-27-2007, 09:42 PM
In this case. It still requires three lines of code and a considerably greater amount of resources in order to perform a single assignment, as well as being a workaround that shouldn't be necessary, for an issue that shouldn't exist (it being the result of a misguided "shortcut").

jscheuer1
10-27-2007, 10:09 PM
Yes, the name 'action' should be avoided in the first place, even if that means changing other code extensively. Here's a better method than removing the node though:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function chng_a(el){
for (var i = 0, a=[], p=el.form.elements; i < p.length; i++)
if(p[i].name&&p[i].name.toLowerCase()=='action'){
a.push([p[i], p[i].name]);
p[i].removeAttribute('name', 0);
}
el.form.action='http://www.google.com';
for (var i = 0; i < a.length; i++)
a[i][0].setAttribute('name', a[i][1], 0);
}
</script>
</head>
<body>
<form action="#" name="regform">
<div><input type="hidden" name="action" value="createnew">
<input type="button" onclick="chng_a(this);" value="Change Action"><br>
<input type="submit" value="Go!"></div>
</form>
</body>
</html>

Twey
10-27-2007, 10:35 PM
Somewhat better, yes.

jscheuer1
10-27-2007, 10:55 PM
You know though, forms are funny. While I was working these ideas out, I discovered that even after removing the name attribute, and even after removing the named element, some browsers would report it as form.action, even though it was no longer there, and even though when asked to change form.action they'd get it right (change the form tag's action attribute).

Just goes to show you how poor forms implementation is in some browsers. FF is the only one that seemed to 'get it 100% right'. I did test Safari Win, it too 'got it wrong', but changed the form's action attribute as desired anyway.

gnznroses
10-27-2007, 11:20 PM
thanks guys.

both of those methods do work, the only problem was that in IE7, i had to click the stupid security bar and allow the content before it'd do anything...

i ultimately went with another solution given on a different forum. it's actually quite easy, it just required looking at the problem a different way.

document.getElementById('actn').name='action';

that's all that was needed ;)
i can't change the script that processes the form data, but of course i can change the form itself, so i just start the input out with a name of "actn" and change it after i've modified the form's action.

Twey
10-27-2007, 11:35 PM
Why do you change it? Why not just stick with "actn"? You're going to have to check that in your server-side script anyway.

gnznroses
10-27-2007, 11:45 PM
i can't modify the script that processes the form data, because it's encrypted.
starting it with a name of "actn" and changing it to how it should be ("action") before the form gets submitted will keep everything working as it should.

Twey
10-28-2007, 12:00 AM
i can't modify the script that processes the form data, because it's encrypted.Yer what? That's ridiculous. How do you know it's doing what you think it is? It might be taking over your server.

gnznroses
10-28-2007, 12:07 AM
it's a commercial script and it's the best one for what i need, so it'll hafta do for now. i mean how many people, when using a huge script, pick apart all of the source code? so any script you download could be harmful. ditto for any computer program. i'd love if it were not encrypted, but it's not a huge deal for me. just a pain to add functionality to in workaround ways like this.