跟踪数据库表/列以进行 html 表单控制的最佳方法

发布于 2024-08-04 03:07:24 字数 415 浏览 5 评论 0原文

我正在开发一个 php 程序,每个表单控件都需要 javascript。一旦 onblur 事件发生,该值将自动发送到服务器以更新数据库。

我可能有 50 个表单控件分布在 5 个选项卡上,因此我不想在 php 文件上硬编码信息。

我可以从 HTML5 中选择一个想法并为此创建新属性例如

data-table='user' data-column='firstname'

对于每个项目,然后 javascript 可以提取这些值并将它们发送到 php 文件。

我不知道是否有更好的方法来管理这些表单控件和几个表/列之间的映射?

I am working on a php program that requires javascript on every form control. Once the onblur event takes place the value will automatically be sent to the server to update the database.

I have possibly 50 form controls spread over 5 tabs so I don't want to hardcode the information on the php file.

I could co-opt an idea from HTML5 and create new properties for this such as

data-table='user' data-column='firstname'

for every item, and then javascript could pull those values out and send them to the php file.

I don't know if there is a better way to manage the mapping between these form controls and the several tables/columns?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烦人精 2024-08-11 03:07:24

HTML

<input id='data-base*data-colum' onblur='preupdate(this);' value='' />

JavaScript

function preupdate(el){
   var idData = el.getAttribute('id').split('*');
   var dataBase = idData[0];
   var dataColum = idData[1];

   update(el.value, dataBase, dataColum);
}

HTML

<input id='data-base*data-colum' onblur='preupdate(this);' value='' />

Javascript

function preupdate(el){
   var idData = el.getAttribute('id').split('*');
   var dataBase = idData[0];
   var dataColum = idData[1];

   update(el.value, dataBase, dataColum);
}
勿忘初心 2024-08-11 03:07:24

事实上我认为这是你最好的选择。如果你使用jquery,你可以这样做:

$("input").blur(function () {
    var data-table = $(this).attr('data-table');
    var data-column = $(this).attr('data-column');

    $.ajax({
    type: 'post',
    url: your_url,
    data: your_get_data
    });
});

Actually I think that's the best option you have. If you use jquery, you can do something like this :

$("input").blur(function () {
    var data-table = $(this).attr('data-table');
    var data-column = $(this).attr('data-column');

    $.ajax({
    type: 'post',
    url: your_url,
    data: your_get_data
    });
});
鸵鸟症 2024-08-11 03:07:24

我将使用 PHP 生成一个数据结构,将控件(希望它们具有唯一的名称/ID)映射到数据库信息。然后我会以 JSON 的形式将其作为 .

然后,所有字段上的 on_blur 属性可以相同(或者每个字段都传递一个唯一的 id),并调用一个 JavaScript 函数从数据结构中查找数据库信息。

I'd use PHP to generate a data structure mapping the controls (hopefully they have unique names/ids) to the db info. Then I'd spit this out in JSON as inline javascript in .

Then your on_blur attributes on all your fields can be the same (or each pass a unique id for that field) and call a javascript function that looks up the db info from the data structure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文