简体   繁体   English

点击浏览器后退按钮时提交表单

[英]submit a form when hitting the browser back button

I have a page for asking queries to an SQL database. 我有一个页面询问SQL数据库的查询。 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. 但是,如果学生使用后退按钮或前进按钮(在按下后退按钮后)数据会丢失,因为我清理了$ _POST变量内容以准备好采取新操作。

There is, however, a "go back" button that assembles data to restore the previous page by POSTing the required data. 但是,有一个“返回”按钮,通过POST所需的数据来组装数据以恢复上一页。 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? 是否有可能使用某种技术,javascript,html5,PHP或其他什么来实际提交在点击浏览器后退按钮时发布汇编数据的表单?

I am using HTML 5, PHP 5 and some JavaScript (not JQuery but if it gives me an option ...) 我正在使用HTML 5,PHP 5和一些JavaScript(不是JQuery,但如果它给了我一个选项...)

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 你可以使用html5存储,因为如果用户没有填写完整的表格或关闭浏览器,数据将在关闭的浏览器上丢失而不是提交表单不填写

to check html5 storage 检查html5存储

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

use onkeyup to store like 使用onkeyup存储就像

  $("#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");

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM