HTML form submission via XMLHttpRequest

Discussion in 'Javascript' started by patrick73, Jul 13, 2006.

  1. patrick73

    patrick73 Guest

    I would like to submit a standard html form using the XMLHttpRequest
    object. As far as I can tell there is no easy way to do this. What I
    have to do is:
    1 Register an event handler on the forms submit event
    2 In this handler I have to:
    2.1 Prevent the default action so that the form does not submit in
    the current window
    2.2 text/x-www-form-urlencode all the form element name value pairs
    following the rules at:
    http://www.w3.org/TR/html4/interact/forms.html#submit-format
    2.3 Build an XMLHttpRequest to the form action uri and then send all
    the encoded elements

    All this is fine, except that at:
    http://www.w3.org/TR/html4/interact/forms.html#submit-format
    the standard specifies that:
    "If a form contains more than one submit button, only the activated
    submit button is successful" and hence only this submit buttons name
    value pair has to be submitted.

    There does not appear to be any standard way to determine which input
    submit element was active during the submit! However, the form.submit()
    method knows which element was active so there must be a way (without
    registering event handlers on all the input submit buttons).

    IE has the unstandard document.activeElement property, is there a
    standard property somewhere? Is there an equivalent to this in gecko?
    patrick73, Jul 13, 2006
    #1
    1. Advertising

  2. patrick73

    Guest

    Straight off the bat, what I'd do is add onclick event handler to the
    submit buttons:

    <input type="submit" value="Send1" onclick="formSubmit(this") />
    <input type="submit" value="Send2" onclick="formSubmit(this") />

    then in

    function formSubmit(button) {

    // add value of button clicked to what is getting sent.
    //"submit" : button.value
    }
    , Jul 13, 2006
    #2
    1. Advertising

  3. patrick73

    patrick73 Guest

    Well yes, that is how I do it at the moment, but I want to avoid that
    (in my case it is not always possible). This information must be sotred
    somewhere, otherwise the form.submit() method would not work. IE stores
    in at least document.activeElement, but where does gecko (or standards
    compliant DOM) store it?

    wrote:
    > Straight off the bat, what I'd do is add onclick event handler to the
    > submit buttons:
    >
    > <input type="submit" value="Send1" onclick="formSubmit(this") />
    > <input type="submit" value="Send2" onclick="formSubmit(this") />
    >
    > then in
    >
    > function formSubmit(button) {
    >
    > // add value of button clicked to what is getting sent.
    > //"submit" : button.value
    > }
    patrick73, Jul 13, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noozer
    Replies:
    7
    Views:
    162
    Noozer
    Apr 15, 2006
  2. Jared
    Replies:
    5
    Views:
    204
    Jared
    Jul 10, 2003
  3. PerlFAQ Server
    Replies:
    0
    Views:
    115
    PerlFAQ Server
    Mar 16, 2011
  4. Slipperman

    POSTing html via XmlHttpRequest

    Slipperman, Aug 9, 2005, in forum: Javascript
    Replies:
    2
    Views:
    169
    Slipperman
    Aug 10, 2005
  5. Noozer
    Replies:
    7
    Views:
    94
    Noozer
    Apr 15, 2006
Loading...

Share This Page