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

标签 javascript php jquery

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

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

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

源代码:https://github.com/kiranbhattarai/Global-Ajax.git
//更改./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 上,请检查上面的链接。

最佳答案

使用LocalStorage数据保存在浏览器中。

//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({
        //...

关于javascript - 如果用户决定返回,则在 ajax 请求后检索 Post 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53908132/

相关文章:

javascript - 如何在触发器单击两次或 jquery 中的外部元素时关闭通知

javascript - 如何隐藏元素,然后在单击包含它们的 DIV 时显示它们

javascript - npm 强硬cookie : save a cookie and then dump it

javascript - 是否有 'shorter' 方法来定义列表,例如100 个新阵列?

php - PHP 5.3 的拼写正确吗?

php - 访问由 while 循环生成的特殊 div

javascript - 如何让函数理解单个整数等于 1

php - 现在 Laravel 5.3 中 save() 返回 false

Jquery数据表集成错误?

javascript - Node.JS 和 Jquery - typeError createWindow is not a function