After Submitting a Post Form Open a New Window Showing the Result

After submitting a POST form open a new window showing the result


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


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

to your form's definition.

Display Results of Form Submission on New Tab or Window

You are never submitting the form.

You have an event handler that, when the form starts to submit, sets location and prevents the normal form submission.

Use instead of that if you want to open a new window.

How to submit form to new window?

I've marked this as duplicate because I thought your problem was about how to send a form to a popup window. Here is how to do it without using the onsubmit event:

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

function submitToPopup(f) {
var w ='', 'form-target', 'width=600, height=400, any-other-option, ...'); = 'form-target';



So instead of using the onsubmit event to create the popup from there, you create it first, just before sending the form, and then send the form to it.

Open a new window & then submit form

I would keep the '_blank' to trigger the new tab/window and the use jQuery and Ajax to submit the form...

A standard html page with a form...


<form id="myForm">
<input name="input1" type="text" value="somevalue1"/>
<input name="input2" type="text" value="somevalue2"/>
<a href="whereEverYouWantToGo.php" target="_blank" id="myFormButton">Send</a>

<input id="result1" value=""/> <?php //use if only wanting to get result back ?>

jQuery used for this page...


$(document).ready(function() {
$(document).on("click","#myFormButton", function() {

var str = $("#myForm").serialize(); // This grabs all your form inputs by name and creates a string e.g. input1=someValue1&input2=someValue2 which you can use later for grabbing the $_GET variables.

cache: false,
url: 'ajax/your-ajax-page-to-submit-the-form.php?'+str,
type: 'POST',
dataType: 'json',
success: function(result) {

// Can get data from Ajax file by using array cretaed in the file (see below)

error : function () {

Now the Ajax page...


ob_start(); //at the very beginning start output buffereing

$var_input1 = mysqli_real_escape_string($dbc, $_GET['input1']);
$var_input2 = mysqli_real_escape_string($dbc, $_GET['input2']);

$q = "INSERT INTO my_table (column1, column2) VALUES ('$input1', '$input2')";
$dbc = mysqli_connect('localhost', 'root', 'password', 'DB_Name') OR die('Could not connect because: '.mysqli_connect_error());
$r = mysqli_query($dbc, $q);

// bof if you want to output any results or values back to jQuery
$var = array(
'result1_from_ajax' => $var_input1,
'result2_from_ajax' => $var_input2
// eof if you want to output any results or values back to jQuery

// Must have
ob_end_clean(); // right before outputting the JSON, clear the buffer.
echo json_encode($var);

How to give the choice to open a new window or stay on the same on form submit?

You can't submit a form when the user right-clicks and chooses "Open in new window."

You might implement a drop-down button where the main part submits to the current window but there's a part that's a drop-down arrow offering the choice to submit to a new window (like this example from Bootstrap).

The "submit to new window" would update target before submitting, obviously.

Here's a really crude example (on jsFiddle — Stack Snippets don't allow forms in any way or shape):

<form action="">
<label>Search: <input type="text" name="q" value="kittens"></label>
<div class="button-menu">
<input type="submit" class="current-window" value="Submit">
<span class="menu-toggler">V</span>
<div class="button-menu-content">
<input type="submit" class="new-window" value="Submit to New Window">


$(".button-menu .menu-toggler").on("click", function() {
$(".new-window").on("click", function() {
var form = $(this).closest("form");
form.prop("target", "_blank");
$(".current-window").on("click", function() {
var form = $(this).closest("form");
form.prop("target", "_self");

Obviously, again, that's very crude, your UI toolkit (whether homebrew or one of the standard ones) will let you make it look a lot smarter.

(How) can I open the result of a form submission in a new window?

<form ... target="windowName">


<form ... target="windowName" onsubmit=",, '...attributes...');return true;">

...attributes... can consist of the stuff documented at the mozilla developer center or MSDN

Why form submit opens new window/tab?

Problem was in base tag. I had intentionally target="_blank" in tag, probably because I copied the example from w3schools :-/

Related Topics

Leave a reply