views:

28786

answers:

5

http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit shows you how to submit a form that you create via JavaScript via post. Below is my modified code.

var form = document.createElement("form");
      form.setAttribute("method", "post");
      form.setAttribute("action", "test.jsp");

          var hiddenField = document.createElement("input");  
          hiddenField.setAttribute("name", "id");
          hiddenField.setAttribute("value", "bob");
          form.appendChild(hiddenField);
          document.body.appendChild(form);    // Not entirely sure if this is necessary   
          form.submit();

What I would like to do is open the results in a new window. I am currentley using something like this to open a page in a new window:

onclick=window.open(test.html,'','scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,status=no');
+22  A: 

Add

<form target="_blank" ...></form>

or

form.setAttribute("target", "_blank");

to your form's definition.

liggett78
+7  A: 

If you want to create and submit your form from Javascript as is in your question and you want to create popup window with custom features I propose this solution (I put comments above the lines i added):

var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "test.jsp");

// setting form target to a window named 'formresult'
form.setAttribute("target", "formresult");

var hiddenField = document.createElement("input");              
hiddenField.setAttribute("name", "id");
hiddenField.setAttribute("value", "bob");
form.appendChild(hiddenField);
document.body.appendChild(form);

// creating the 'formresult' window with custom features prior to submitting the form
window.open(test.html, 'formresult', 'scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,status=no');

form.submit();
Marko Dumic
+1 Better than the accepted answer, since it actually puts the result in the popup with the options the OP wanted.
Renesis
A: 

form.nameOfHiddenField in the above case is id

Jas
A: 

I know this basic method:

1)
<input type=”image” src=”submit.png”> (in any place)
2)
<form name=”print”>
<input type=”hidden” name=”a” value=”<?= $a ?>”>
<input type=”hidden” name=”b” value=”<?= $b ?>”>
<input type=”hidden” name=”c” value=”<?= $c ?>”>
</form>
3)
<script>
$(‘#submit’).click(function(){
open(”,”results”);
with(document.print)
{
method = “POST”;
action = “results.php”;
target = “results”;
submit();
}
});
</script>

Works!

Edu Carrega
thats with jquery ! he asks for pure JS
Aviatrix
A: 

im using similar functionality but it looks like creating a javascript form dynamically and then submitting it becomes a problem when the pages requires authentication.. Looks like the session isn't maintained (which would make sense to be honest)..

Not sure what authentication is occuring right now but I'll let you know!!

Cheers,

James Radford Web Developer