5

Consider the following simple form:

<form method="GET" action="handle.php">
  <input type="hidden" name="action" value="search">
</form>

Form submission is performed by Javascript (iui) in an ajax call. All fields are properly gathered from the form. Javascript then wants to send the ajax call to "form.action".

This is where my problem starts. The object form is of type HTMLFormElement. The action property of the form is supposed to be of type string and should contain "handle.php". After some hours of debugging, I noticed that form.action is now of type HTMLInputElement.

My question: Is this proper Javascript behavior? I would have never though that defining a form field with the name of a form attribute, this would happen. In the mean time I solved the issue by naming my field differently.

Thanks in advance for any advice...


Found an easy way of displaying my problem. First the form with the problem:

<form action="test.php">
    <input type="hidden" name="action" value="test">
    <input type="button" onclick="alert(this.form.action);">
</form>

And the form that is proper:

<form action="test.php">
    <input type="hidden" name="NOT_AN_ATTRIBUTE_NAME" value="test">
    <input type="button" onclick="alert(this.form.action);">
</form>

In the first, the popup states "[object HTMLInputElement]", in the second: "http://localhost/test.php".

4
  • Could you please add the javascript code as well Commented Jan 6, 2011 at 13:57
  • I dont know is that proper for JS but it seem its in a way you described, is that big issue to change input name from action to another name? Commented Jan 6, 2011 at 13:59
  • @Chris: obviously I already solved it by renaming my field. But the problem remains when posting forms using javascript in ajax calls. Form fields cannot have the same name as form attributes. Commented Jan 6, 2011 at 14:07
  • 1
    Can't you use document.getElementById('formid').getAttribute('action') instead of just form.action? And similarly for other fields? Commented Jan 6, 2011 at 14:36

3 Answers 3

3

The issue you're seeing is because forms are special in JavaScript. All their fields are accessable as properties, so when you use this.form.action, it's getting the field action, not the HTML attribute action="test.php".

Try changing alert(this.form.action); to alert(this.form.getAttribute('action')) instead.

Sign up to request clarification or add additional context in comments.

1 Comment

/Matthew: getAttribute seems to work. It doesn't give a full URL like this.form.action does, but luckily ajax doesn't care too much.
0

It seems as bug. Maybe there should be an array for 'action '

<form action="test.php">
    <input type="hidden" name="action" value="test">
    <input type="button" onclick="alert(this.form.action[0]);"> //the form action
    <input type="button" onclick="alert(this.form.action[1]);"> // the text input 
</form>

1 Comment

@Matt: Yes, its proposition to solution that would had to be included in Javascript itself.
0

your this.form.action is still a object. instead of using alert put it in a console.log(this.form.action) and use firebug and firequery try to discover what events/properties your this.form.action has.

!you need to enable your console in firebug before you can use console.log

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.