Tabbed Content / Posts /

How to open POST page in the same tab?

0
If I include form inside a tab, and click submit button, POST page opens as a new page. But I want to open it inside the same tab and not as a new page. How can I do that?
Apurvd  10 years ago   viewed: 3996    

4 Answers

0
You may need to read Other Ways to Select a Tab on the page http://www.menucool.com/jquery-tabs#Div3. But it is not free to use.
Julia   10 years ago
0
let me give more clarification.. I want to open a link in the same tab currently the link get opened in as new page within browser.. i want to retain original page and open the link just inside tab..I want same behavior when the form inside tab is POSTed...hope you got it now
Apurvd   10 years ago
0
It sounds like you want to populate the current tab content with the content retrieved from another page. That is Ajax. You can read the Ajax section of Menucool's jQuery Tabs.
Milo   10 years ago
0

To open the POST response page inside the same tab instead of opening it as a new page, you can use JavaScript to handle the form submission and prevent the default behavior of the form.


Here's an example of how you can achieve this that I refer in territorial io


Add an id attribute to your form element, for example:

html

Copy

<form id="myForm" action="submit.php" method="POST">

  <!-- form controls here -->

  <button type="submit">Submit</button>

</form>

Add a JavaScript function that handles the form submission and prevents the default behavior of the form:

javascript

<script>

function submitForm() {

  var form = document.getElementById("myForm");

  form.addEventListener("submit", function(event) {

    event.preventDefault(); // prevent default form submission

    var xhr = new XMLHttpRequest();

    xhr.open("POST", form.action); // set up the POST request

    xhr.onload = function() {

      if (xhr.status === 200) { // check if the response is successful

        // update the content of the current tab with the response

        document.getElementById("myTab").innerHTML = xhr.responseText;

      }

    };

    xhr.send(new FormData(form)); // send the form data

  });

}

submitForm();

</script>

Add an id attribute to the tab that you want to update with the POST response, for example:

html

<div class="tab" id="myTab">

  <!-- tab content here -->

</div>

With these changes, when the user clicks the Submit button, the form will be submitted using the XMLHttpRequest object, which will send the POST request in the background without reloading the page. The response will then be processed in the JavaScript function, and the content of the tab will be updated with the response.

julierodgers
  9 months ago

   

Your name*
Password
(Optional. Used to modify this post afterwords)
+ =  

Ask your Own Question

  • If your question is related to the topic of this post, you can post your question to this page by clicking the "Post a reply" button at left;

  • When you want to start a new page for your question: