简体   繁体   English

如何从动态生成的文本框中保存数据?

[英]How to save data from dynamically generated textboxes?

I have a page to edit the 'utilization' rate of each employee, based on their designations.我有一个页面可以根据每个员工的名称编辑他们的“使用率”。 So the page has all the designations listed as , next to which a textbox, for the user to fill in the utilization rate.因此该页面将所有名称列为 ,旁边是一个文本框,供用户填写使用率。 Scenario is that the user will not save each designation's utilization rate immediately after filling it.场景是用户填写后不会立即保存每个指定的使用率。 User will keep going till he fills the last item and then hit the submit button to save.用户将继续前进,直到填满最后一项,然后点击提交按钮保存。 Now, in php I can get all the values from the Request Array.现在,在 php 中,我可以从请求数组中获取所有值。 But, how will I know which designations these values belong to?但是,我如何知道这些值属于哪些名称? So, what i have as a solution is to name the textboxes with the designation_ids as suffix.所以,我的解决方案是用 designation_ids 作为后缀命名文本框。 May be like;可能就像;

utilTextbox_1, utilTextbox_2, utilTextbox_3 etc... utilTextbox_1、utilTextbox_2、utilTextbox_3 等...

Then when the form is submitted;然后当表单提交时;

Check each Request Array element to see if its name starts with 'utilTextbox'检查每个请求数组元素以查看其名称是否以“utilTextbox”开头

If Yes, split it using '_' to get the designation_id如果是,请使用“_”将其拆分以获取 designation_id

Update the db table with the value of the text box用文本框的值更新db表

Check the next Array Element.....and so on...检查下一个数组元素......等等......

is this the correct method or is there a better way of doing this?这是正确的方法还是有更好的方法?

You can have array as names in html forms.您可以将数组作为 html 表单中的名称。 So something like utilTextbox[1], utilTextbox[2], utilTextbox[3] will work perfectly fine.所以像 utilTextbox[1], utilTextbox[2], utilTextbox[3] 这样的东西会工作得很好。 In php you will get an array in $_REQUEST and you need not to convert in an array because it is already an array.在 php 中,您将在 $_REQUEST 中获得一个数组,您无需在数组中进行转换,因为它已经是一个数组。

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

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