简体   繁体   English

异步保存数据php

[英]Save data asynchronously php

Today one of my client required that he need to save the form data to the server asynchronously when it CHANGES 今天我的一个客户要求他需要在更改异步地将表单数据保存到服务器

ex : 例如:

i have a form for filling the user details ,including text box,select,radio,check etc... 我有一个填写用户详细信息的表单,包括文本框,选择,收音机,支票等...

so when i change a value in text box i need to save them back to server. 所以当我在文本框中更改值时,我需要将它们保存回服务器。

is there any libraries or something available on php that i can use ? 我可以使用PHP上的任何库或东西吗?

Please note i know what is ajax,but still i need to code so much manually,also i care re-usability. 请注意我知道什么是ajax,但我仍然需要手动编写代码,我还需要重新使用。 Thats why i am asking for a library. 这就是我要求图书馆的原因。

Edit 编辑

I know jQuery,Js,Ajax etc ..but my idea is different ... 我知道jQuery,Js,Ajax等。但我的想法不同......

say that 比如说

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

sure i can do 我相信我能做到

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

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

if(is_changed)
{

//send to the server
}

});

BUT I AM SEARCHING FOR A LIBRARY THAT CAN DO THIS AUTOMATICALLY 但我正在寻找一个可以自动完成的图书馆

ex: 例如:

//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 on server someFunc.php在服务器上

dataMapper DM = new dataMapper();

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

return $changes

the values is from dataBinder[name] from 值来自dataBinder[name]

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

var_dump($changes); gives

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

I am sure there may be lot of questions about the developer ,but i am just searching if any because i dont want to reinvent the wheel. 我相信可能有很多关于开发人员的问题,但我只是在搜索,因为我不想重新发明轮子。

Why not use a data-save-ajax attribute in every input element you need and add the ajax page as a value. 为什么不在所需的每个输入元素中使用data-save-ajax属性,并将ajax页面添加为值。 Now you create one jQuery (or whatever ajax javascript you prefer) and write an on-change event handler that saves the values to the data-save-ajax link. 现在,您创建一个jQuery(或您喜欢的任何ajax javascript)并编写一个将值保存到data-save-ajax链接的on-change事件处理程序。

Something like this: 像这样的东西:

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

Things to consider is the load this will be generating. 要考虑的是这将产生的负载。 Best is to use an event handler when losing focus. 最好是在失去焦点时使用事件处理程序。 Or if you prefer on-change, then perhaps a few milliseconds before starting to ajax the changes. 或者如果你喜欢改变,那么在开始改变之前可能还需要几毫秒。

The data-save-ajax link will ofcourse be a php page that checks the session and saves the data to the database based on the url. data-save-ajax链接将是一个php页面,它检查会话并根据url将数据保存到数据库。 You can use .htaccess (or other) rewriting to let your php know what input you are using. 您可以使用.htaccess(或其他)重写来让您的php知道您正在使用的输入。

For instance a data-save-ajax url of /ajax/text/name will be rewritten to /ajax/index.php . 例如, /ajax/text/name的data-save-ajax url将被重写为/ajax/index.php

I once wrote an ajax updater like this, it's easily built and you can update or re-use it easily anytime you have need of it. 我曾经写过像这样的ajax更新程序,它很容易构建,你可以随时随地更新或重新使用它。

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

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