Faking a form submission

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Smith

    Faking a form submission

    Good morning,

    Is there a good way to use JavaScript to send a form submission, but get
    back the response as a string, rather than loading it into a page? I
    could write the code to send the submission via XMLHttpRequest, but that
    seems hard... especially implementing multipart-encoded form submission
    such as required for file upload.

    Ideas?

    --

    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
  • Duncan Booth

    #2
    Re: Faking a form submission

    Chris Smith wrote:
    [color=blue]
    > Good morning,
    >
    > Is there a good way to use JavaScript to send a form submission, but get
    > back the response as a string, rather than loading it into a page? I
    > could write the code to send the submission via XMLHttpRequest, but that
    > seems hard... especially implementing multipart-encoded form submission
    > such as required for file upload.
    >
    > Ideas?
    >[/color]
    You might want to have a look at kupu (SVN checkout from
    http://codespeak.net/svn/kupu/trunk/kupu) and search for kupu_upload_for m.

    The form there has a target attribute and is followed by an invisible
    iframe with a name matching the target. When the form is submitted, this
    means the response comes back into the iframe. The response includes an
    onLoad event which calls a javascript method back in the main document to
    handle the result or display an error message.

    Comment

    • Chris Smith

      #3
      Re: Faking a form submission

      Duncan Booth <duncan.booth@i nvalid.invalid> wrote:[color=blue]
      > You might want to have a look at kupu (SVN checkout from
      > http://codespeak.net/svn/kupu/trunk/kupu) and search for kupu_upload_for m.
      >
      > The form there has a target attribute and is followed by an invisible
      > iframe with a name matching the target. When the form is submitted, this
      > means the response comes back into the iframe. The response includes an
      > onLoad event which calls a javascript method back in the main document to
      > handle the result or display an error message.[/color]

      That would work... except that I don't have any control over the form
      tag... and the form needs to work as usual when the real submit button
      is pressed. I suppose I could temporarily set the target via DOM, and
      then put it back after the Form.submit() call... seems kinda kludgy,
      though. I'm debating whether that's cleaner than faking the form submit
      via XMLHttpRequest.

      --

      The Easiest Way To Train Anyone... Anywhere.

      Chris Smith - Lead Software Developer/Technical Trainer
      MindIQ Corporation

      Comment

      • Duncan Booth

        #4
        Re: Faking a form submission

        Chris Smith wrote:
        [color=blue]
        > Duncan Booth <duncan.booth@i nvalid.invalid> wrote:[color=green]
        >> You might want to have a look at kupu (SVN checkout from
        >> http://codespeak.net/svn/kupu/trunk/kupu) and search for
        >> kupu_upload_for m.
        >>
        >> The form there has a target attribute and is followed by an invisible
        >> iframe with a name matching the target. When the form is submitted,
        >> this means the response comes back into the iframe. The response
        >> includes an onLoad event which calls a javascript method back in the
        >> main document to handle the result or display an error message.[/color]
        >
        > That would work... except that I don't have any control over the form
        > tag... and the form needs to work as usual when the real submit button
        > is pressed. I suppose I could temporarily set the target via DOM, and
        > then put it back after the Form.submit() call... seems kinda kludgy,
        > though. I'm debating whether that's cleaner than faking the form
        > submit via XMLHttpRequest.
        >[/color]
        You may not have to put it back immediately: you just need an onsubmit
        handler which works out which way you want to submit the form and direct
        the target accordingly.

        Alternatively clone the entire form and set the target on the clone. I have
        no idea whether you can then submit the clone without adding it into the
        DOM. If you can then that would be a nice solution, one call to cloneNode,
        set the target and submit.

        Comment

        Working...