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

javascript

To submit a POST form and open a new window showing the result, you can use JavaScript to create a new window and populate it with the result of the form submission. Here’s an example:

HTML:

<form id="myForm" method="POST" action="submit.php">
  <input type="text" name="username">
  <input type="submit" value="Submit">
</form>

JavaScript:

// Attach event listener to form submit event
$("#myForm").submit(function(event) {
  // Prevent default form submission behavior
  event.preventDefault();
  
  // Perform form submission using AJAX
  $.post($(this).attr("action"), $(this).serialize(), function(data) {
    // Create new window and populate it with the result of the form submission
    var newWindow = window.open("", "Result", "width=600,height=400");
    newWindow.document.write(data);
  });
});

In this example, we use jQuery to attach an event listener to the submit event of the myForm form. We prevent the default form submission behavior using event.preventDefault() to avoid navigating away from the current page. We then use jQuery’s $.post() method to perform an AJAX form submission to the URL specified in the form’s action attribute. The data parameter in the callback function contains the response from the server.

Finally, we create a new window using window.open() and pass in a window name and dimensions. We then use the document.write() method to populate the new window with the result of the form submission.

Note that this example assumes you have included jQuery in your page. If you prefer not to use jQuery, you can use the XMLHttpRequest object to perform the AJAX form submission instead.