SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Submitting Dynamically Created Forms

    JavaScript is not my best language, so please pardon me if this is a silly question.

    How do you submit a dynamically created form in a browser?

    Here is my code:

    Code:
    function generateWGetScript(event) {
      var wgetForm = document.createElement("form");
      wgetForm.id = "wgetFormId";
      wgetForm.name = "wgetFormName";
      wgetForm.action = "../download/generateWGetScript.htm";
      wgetForm.method = "POST";
      wgetForm.target = "_blank";
      
      var files = document.createElement("input");
      files.type = "hidden";
      files.name = "logicalFileIdentifiers";
      files.setAttribute("value", ["test1", "test2"]);
      
      wgetForm.submit();
    }
    The problem above, is that the Form is never actually submitted. All the code does run though. I placed an alert msg box after the wgetForm.submit() and it is always called.

    However, the following code does work and the form is submitted as hoped:

    Code:
    function generateWGetScript(event) {
      var wgetForm = document.getElementById("wgetFormId");
      
      var files = document.createElement("input");
      files.type = "hidden";
      files.name = "logicalFileIdentifiers";
      files.setAttribute("value", ["test1", "test2"]);
      
      wgetForm.submit();
    }
    Then the above form is declared inside of the <body> </body> tags somewhere on the page...

    Code:
    ...
    <form id="wgetFormId" action="../download/generateWGetScript.htm" method="POST" ></form>
    ...
    What is going on here? Why doesn't the first example seem to work?

    Thank you for your time.
    baby steps... baby steps...

  2. #2
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suspect that the form is submitted in both cases.
    Could you please try to comment the wgetForm.target = "_blank"; line? There may be a chance that this line specifies that the submit result should be opened in a pop-up window and browser just silently disables this window.
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  3. #3
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the quick reply.

    I commented out the target = "_blank" line and the form still did not submit.

    I can tell it doesn't submit because I'm watching all the browser's requests via the LiveHTTPHeaders plug-in for Firefox.

    Thank you for your time.
    baby steps... baby steps...

  4. #4
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. Another issue that I've noticed is about adding element on the form.
    The files input is not added on the wgetForm form. It should be added with
    Code:
    wgetForm.appendChild(files);
    command.
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  5. #5
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are correct. I was not adding the hidden input field. That has been corrected.

    Thank you for pointing that out.
    baby steps... baby steps...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •