How do you stay on the same page after submit in javascript?

I have a form that looks like this

<form action="receiver.pl" method="post">
  <input name="signed" type="checkbox">
  <input value="Save" type="submit">
</form>

and I would like to stay on the same page, when Submit is clicked, but still have receiver.pl executed.

How should that be done?

asked Apr 20, 2011 at 16:48

2

99% of the time I would use XMLHttpRequest or fetch for something like this. However, there's an alternative solution which doesn't require javascript...

You could include a hidden iframe on your page and set the target attribute of your form to point to that iframe.

<style>
  .hide { position:absolute; top:-1px; left:-1px; width:1px; height:1px; }
</style>

<iframe name="hiddenFrame" class="hide"></iframe>

<form action="receiver.pl" method="post" target="hiddenFrame">
  <input name="signed" type="checkbox">
  <input value="Save" type="submit">
</form>

There are very few scenarios where I would choose this route. Generally handling it with javascript is better because, with javascript you can...

  • gracefully handle errors (e.g. retry)
  • provide UI indicators (e.g. loading, processing, success, failure)
  • run logic before the request is sent, or run logic after the response is received.

answered Apr 20, 2011 at 16:52

jessegavinjessegavin

72.1k27 gold badges137 silver badges164 bronze badges

7

The easiest answer: jQuery. Do something like this:

$(document).ready(function(){
   var $form = $('form');
   $form.submit(function(){
      $.post($(this).attr('action'), $(this).serialize(), function(response){
            // do something here on success
      },'json');
      return false;
   });
});

If you want to add content dynamically and still need it to work, and also with more than one form, you can do this:

   $('form').live('submit', function(){
      $.post($(this).attr('action'), $(this).serialize(), function(response){
            // do something here on success
      },'json');
      return false;
   });

Riley Lark

20.4k14 gold badges79 silver badges126 bronze badges

answered Apr 20, 2011 at 16:52

Herman SchaafHerman Schaaf

44.6k20 gold badges98 silver badges137 bronze badges

25

The HTTP/CGI way to do this would be for your program to return an HTTP status code of 204 (No Content).

How do you stay on the same page after submit in javascript?

answered May 7, 2013 at 12:36

5

When you hit on the submit button, the page is sent to the server. If you want to send it async, you can do it with ajax.

How do you stay on the same page after submit in javascript?

Paritosh

1,0631 gold badge12 silver badges29 bronze badges

answered Apr 20, 2011 at 16:51

How do you stay on the same page after submit in javascript?

Eric FrickEric Frick

8351 gold badge7 silver badges17 bronze badges

Use XMLHttpRequest

var xhr = new XMLHttpRequest();
xhr.open("POST", '/server', true);

//Send the proper header information along with the request
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

xhr.onreadystatechange = function() { // Call a function when the state changes.
    if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
        // Request finished. Do processing here.
    }
}
xhr.send("foo=bar&lorem=ipsum");
// xhr.send(new Int8Array()); 
// xhr.send(document);

answered Jan 16, 2019 at 20:49

How do you stay on the same page after submit in javascript?

Hasan A YousefHasan A Yousef

20.5k22 gold badges119 silver badges180 bronze badges

How do I get javascript to stay on the same page?

Bookmark this question. Show activity on this post. onClick=validateForm() it shows alert if it is blank but it goes to next page("action=some.

How do I stay on the same page after clicking?

You can use preventDefault() on the click event for a given html object. This will prevent the browser from taking the default action on a specific HTML object.

How do you stay on the same page after submit in HTML?

In order to stay on the same page on submit you can leave action empty ( action="" ) into the form tag, or leave it out altogether. For the message, create a variable ( $message = "Success!

How can I submit a form without refreshing the page?

Submit a Form Without Page Refresh Using jQuery.
Build the HTML Form. Let's take a look at our HTML markup. ... .
Begin Adding jQuery. The next step in the process is to add some jQuery code. ... .
Write Some Form Validation. ... .
Process Form Submission With the jQuery AJAX Function..