Javascript 如果用户决定返回,则在ajax请求之后检索Post数据

Javascript 如果用户决定返回,则在ajax请求之后检索Post数据,javascript,php,jquery,Javascript,Php,Jquery,说明:请检查代码,这样会更有意义 我已成功创建了一个函数,该函数将执行以下操作: 1.它将使用class=“ajax” 2.它将查找属性(操作、方法、名称) 3.index.php上的表单有两个名称属性:fname和lname 4.如果名称字段不为空,则它将从index.php文件向ajaxphp/save.php发出post请求,其中fname和lname的值从表单中隐藏,并且表单还有另一个名为email的输入字段 5.当表单从ajaxphp/save.php提交时,它将再次向../home.

说明:请检查代码,这样会更有意义

我已成功创建了一个函数,该函数将执行以下操作:
1.它将使用class=“ajax”
2.它将查找属性(操作、方法、名称)
3.index.php上的表单有两个名称属性:fname和lname
4.如果名称字段不为空,则它将从index.php文件向ajaxphp/save.php发出post请求,其中fname和lname的值从表单中隐藏,并且表单还有另一个名为email的输入字段
5.当表单从ajaxphp/save.php提交时,它将再次向../home.php发出post请求,在那里可以看到所有post变量。
6.我还在./ajaxphp/save.php返回上添加了一个新按钮

问题: 如果用户在index.php处填写表单并转到ajaxphp/save.php,并决定通过单击“返回”按钮再次返回index.php,则我会填充用户已插入并提交的fname和lname字段

源代码:
//更改./classes/config.php,它将在本地主机上完美工作

我已经尝试将index.php中的post请求插入到数据库中,并在他们单击返回按钮时再次将其拉回来。但是我正在寻找一种方法来实现ajax,而不需要数据库

$('form.ajax').on('submit',function () {

    var that = $(this),         //$this is a reference to the calling object of forms with class .ajax
    url = that.attr('action'),  //represents the action specified on the form
    type = that.attr('method'), //reperesents the method specified on the form

    data = [];                  //creating an array to store the values

    that.find('[name]').each(function(index,value){  //find() function finds all the value with the name attribute and each() function loops through all the inputs files that has name attribure
        var that = $(this),         //$this is a reference to the calling object of forms input name attr
        name = that.attr('name'),  //assigning name of the input field to a variable name
        value = that.val();        //assigning the value input filed to a variable value
        data[name] = value;        //assigning the respective name value pair
    });

  //  console.log(data);

if(data['fname']=='' || data['lname']==''){   //checking if the fields are empty or not
      alert('Please fill all the fields');
}
else{
    $.ajax({                //Submitting the ajax request
        url: url,
        type: type,
        data: data,
        success: function(response){
            console.log(response);
        }

    });
}
    return false;  
});
我已经尽力通过正确解释所有函数来记录代码。它们位于git hub上,请检查上面的链接。

用于在浏览器中保存
数据

//for reading
var data;
if (localStorage.getItem('data')) {
  data = JSON.parse(localStorage.getItem('data'));
  // do actions
  // console.log(data['fname'])
}

// for writing
$('form.ajax').on('submit', function() {
      // ...
      else {
        var jsonStr = JSON.stringify(data);
        localStorage.setItem('data', jsonStr);
        $.ajax({
        //...

您可以在当前窗口中定义和访问全局变量: