繁体   English   中英

异步保存数据php

[英]Save data asynchronously php

今天我的一个客户要求他需要在更改异步地将表单数据保存到服务器

例如:

我有一个填写用户详细信息的表单,包括文本框,选择,收音机,支票等...

所以当我在文本框中更改值时,我需要将它们保存回服务器。

我可以使用PHP上的任何库或东西吗?

请注意我知道什么是ajax,但我仍然需要手动编写代码,我还需要重新使用。 这就是我要求图书馆的原因。

编辑

我知道jQuery,Js,Ajax等。但我的想法不同......

比如说

<input type="text" class="dataBinder[name]" id="cname" name="name" />
<input type="checkbox" class="dataBinder[agree_me]" id="agree_me" name="agree_me" />

我相信我能做到

 $("#cname").event(function(){

 var is_changed = true; ///check for changes if any

if(is_changed)
{

//send to the server
}

});

但我正在寻找一个可以自动完成的图书馆

例如:

//assume a library dataMapper

var myForm = $("#myForm"); //get the form

myForm.dataMapper({

"server" :"serverUrl/someFunc.php"
//may be some other kind of option too
});

someFunc.php在服务器上

dataMapper DM = new dataMapper();

$changes = DM->get_data(); //a function that which give the changes

return $changes

值来自dataBinder[name]

<input type="text" class="dataBinder[name]" id="cname" name="name" />

var_dump($changes);

array(3) {
  ["changes"]=>
  array(2) {
    ["cname"]=>
    string(15) "my-changed-name"
    ["agree"]=>
    bool(false)
  }
  ["time"]=>
  string(6) "720124"
  ["form"]=>
  string(6) "formId"
}

我相信可能有很多关于开发人员的问题,但我只是在搜索,因为我不想重新发明轮子。

为什么不在所需的每个输入元素中使用data-save-ajax属性,并将ajax页面添加为值。 现在,您创建一个jQuery(或您喜欢的任何ajax javascript)并编写一个将值保存到data-save-ajax链接的on-change事件处理程序。

像这样的东西:

jQuery("[data-save-ajax]").change( function() {
    jQuery.ajax({
        url: jQuery(this).attr("data-save-ajax"),
        method: 'post',
        data: {
            newvalue: jQuery(this).val()
        }
    });
});

要考虑的是这将产生的负载。 最好是在失去焦点时使用事件处理程序。 或者如果你喜欢改变,那么在开始改变之前可能还需要几毫秒。

data-save-ajax链接将是一个php页面,它检查会话并根据url将数据保存到数据库。 您可以使用.htaccess(或其他)重写来让您的php知道您正在使用的输入。

例如, /ajax/text/name的data-save-ajax url将被重写为/ajax/index.php

我曾经写过像这样的ajax更新程序,它很容易构建,你可以随时随地更新或重新使用它。

暂无
暂无

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

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