简体   繁体   中英

Submit form from another page with Javascript

I have a problem when I try to submit a form from another page, which is loaded in a DOM element. The stated form element has following attributes and looks like this:

<form id="thisForm" enctype="multipart/form-data" method="post" action="some.php" accept-charset="UTF-8">
<table>
<tbody>

<tr>
<th><b>Title 1</b></th>
<td><select name="name1" id="id1"><option value="1">Selection 1</option><option value="2">Selection 2</option></select></td>
</tr>

<tr>
<th><b>Title 2</b></th>
<td><textarea name="name2" id="id2"></textarea></td>
</tr>

<tr>
<th><b>Title 3</b></th>
<td><select name="name3" id="id3"><option value="11">Selection 11</option><option value="12">Selection 12</option></select></td></tr>
<tr>

<td><input type="submit" name="__saveForm" value="Save"></td>
</tr>

</tbody>
</table>
</form>

And following the JS code, sorry for bad conventions:

var formwrap = document.createElement("div");
var formform = document.createElement("form");

$.get("some.php", null, function(html) {
    var htmlstring = html;
    formwrap.innerHTML= htmlstring;
    formform = formwrap.querySelector("#thisForm");

    document.body.appendChild(formform); // I need to append the form somewhere to the current page, or I get this error in chrome: "Form submission canceled because the form is not connected"

    formform.querySelector("#id1").value = '2';     // Set a value in form
    formform.querySelector("#id3").value = '12';    // Set a value in form

    formform.submit();

    }, "text");

When the code executes, I get redirected to the page where the form is included (some.php), but the form is not actually submitted. Also the selections I made via code are not present here.

Is there a way to submit this form without having the respective page (some.php) opened in the browser?

Modyfing the source code of the form element is unfortunately not an option, I can only change the javascript part.

Thank you for the help

I was able to fix it.

I checked in Chrome Network Debug that the HTTP POST was sent out, but some parameters were missing compared to when i submit the form manually. I was able to add these via JS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM