After submitting a POST form open a new window showing the result
javascriptTo 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.
Other Article on Tag javascript
- - Adding and removing content in jQuery
- - Are JavaScript strings immutable? Do I need a "string builder" in JavaScript?
- - Can I change the Dojo namespace to something other than dojo
- - Can I prevent user pasting Javascript into Design Mode IFrame
- - dynamically create html element in javascript
- - Find XY of an HTML element with JavaScript
- - How can I upload files asynchronously with jQuery
- - How can you display Typing Speed using Javascript or the jQuery library
- - How do you capture mouse events in firefox
- - How to auto-size an iFrame