serafim
serafim

Reputation: 75

submit a form when hitting the browser back button

I have a page for asking queries to an SQL database. Its only purpose is to allow students to exercise. Depending on the students activity the page rewrites itself with new content so that the student may enter a query, have the resulting table shown or get an error message.

All is working through forms that post data to the same page.

However, if a student uses the back button or the forward button (after hitting the back button) data gets lost as I cleanse the $_POST variable content to get ready for new action.

There is, however, a "go back" button that assembles data to restore the previous page by POSTing the required data. Is it possible to use some kind of technique, javascript, html5, PHP or whatever to actually submit the form that posts the assembled data when hitting the browser back button?

I am using HTML 5, PHP 5 and some JavaScript (not JQuery but if it gives me an option ...)

Upvotes: 1

Views: 1551

Answers (2)

Dale
Dale

Reputation: 10469

As suggested in the comments you could store the post data in the session, for example every time a new query is posted you could add it:

$_SESSION['queries'][] = $_POST;

Then you could allow the users to go back / forward through this with some form of loop:

<ul>
<?php foreach($_SESSION['queries'] as $k => $v) : ?>
    <li>Some link structure</li>
<?php endforeach; ?>
</ul>

Upvotes: 2

NullPoiиteя
NullPoiиteя

Reputation: 57322

you can use the html5 storage since if user not fill the full form or close the browser the data will lost on close browser and not submit it form not fill

to check html5 storage

 function supports_html5_storage() {
          try {
            return 'localStorage' in window && window['localStorage'] !== null;
          } catch (e) {
            return false;
          }
        }

use onkeyup to store like

  $("#title").keyup(function(){

            var articel_title =  $("#title").val();
            localStorage.setItem("articel_title",articel_title);
             localStorage.getItem("articel_title");
        });

and next time when user open the form just show the content stored

to clear use

localStorage.removeItem("articel_title");

Upvotes: 2

Related Questions