简体   繁体   English

使用PHP从动态创建的输入中收集数据

[英]Collect data with PHP from dynamically created inputs

I have this code: http://jsfiddle.net/Zx8hc/9/ . 我有以下代码: http : //jsfiddle.net/Zx8hc/9/

It's a form where a user can add input-text dynamically, he can add and delete as many as he wants (only and the first being mandatory). 用户可以动态添加输入文本,也可以根据需要添加和删除任意数量的表单(只有第一个是强制性的)。 And since he can add and delete the ones he likes their ID's may not be consecutive. 而且由于他可以添加和删除他喜欢的ID,所以这些ID可能不是连续的。

My question is how can I collect the data from the newly created inputs with PHP and store it in vars, each input in it's own var, independently ( [input_1] > $input_1 , so on). 我的问题是如何使用PHP从新创建的输入中收集数据并将其存储在vars中,每个输入都独立存在于其自己的var中( [input_1] > $input_1等)。 My idea was to create a for loop and go through all the elements from 0 to n , and those who contain data would be stored and those who don't wouldn't. 我的想法是创建一个for循环并遍历从0n所有元素,那些包含数据的元素将被存储,那些没有数据的元素将被存储。 Would that be an appropriate approach? 那是一个合适的方法吗?

I am sorry that I don't have any PHP code to show but I don't even know where to start with this one, so thank you very much in advance if you can help me with this one. 很抱歉,我没有任何PHP代码可显示,但我什至不知道从哪里开始,所以如果您能帮助我解决这个问题,请先非常感谢。

I suggest to create these new inputs with name tags. 我建议使用name标签创建这些新输入。 These name tags must be unique eg cool_input1 , cool_input_2 , ... OR use as array: cool_input[] . 这些名称标签必须唯一,例如cool_input1cool_input_2 ,...或用作数组: cool_input[]
As result - you can get incoming info in php and parse received data from POST/GET. 结果-您可以在php中获取传入信息,并从POST / GET中解析收到的数据。

For the first idea you don't need to know real count of the generated inputs. 对于第一个想法,您不需要知道所生成输入的实际计数。 You just can use 'foreach element in POST' and if its name matches your pattern - this is what you need. 您仅可以使用“ POST中的foreach元素”,并且如果其名称与您的模式匹配-这就是您所需要的。

I have checked your fiddle, and you have the next HTML as input element. 我检查了您的小提琴,然后将下一个HTML用作输入元素。

<input type="text">

If you want to send form data to a server, you have to wrap it in a form element. 如果要将表单数据发送到服务器,则必须将其包装在一个form元素中。 Here below is an example of a simple form 以下是一个简单表格的示例

<form action="url_to_php_file.php" method="post">
    <input type="text" name="age[]" />
    <input type="submit" value="submit" />
</form>

Here, you see a <form> element, which you can use to pass form data to the server. 在这里,您看到一个<form>元素,可用于将表单数据传递到服务器。 It has several attributes. 它具有几个属性。 Here, action is the URL to where the form content should be sent to. 这里, action是表单内容应发送到的URL。 Pick it the PHP file where the form should be handled. 选择要处理表单的PHP文件。 If it's on the same page as the display, just use # as field. 如果它与显示屏在同一页面上,只需使用#作为字段。

Then method -attribute is to send the form by POST data. 然后, method -attribute将通过POST数据发送表单。 The other option is using GET, but it's not secure because using GET will send in the form data in the URL too. 另一种选择是使用GET,但这并不安全,因为使用GET也会在URL中发送表单数据。 While POST will wrap it in the request. 而POST会将其包装在请求中。

If you have a form element, it's necessary to have a button to submit the form. 如果您有一个表单元素,则必须有一个按钮来提交表单。 By submitting, you're activating the trigger to send the form to the address described in action-attribute of the form. 通过提交,您正在激活触发器以将表单发送到表单的动作属性中描述的地址。 That's the input-submit element. 那就是input-submit元素。

Now, the data itself. 现在,数据本身。 Each input has to be assigned with a name -attribute. 每个输入都必须分配一个name -attribute。 The content of it will be associated to that name when submitting the form. 提交表单时,其内容将与该名称相关联。 If you want to send in multiple data as one name field, you have to use an array, the [] in the form name. 如果要发送多个数据作为一个名称字段,则必须使用一个数组,即表单名称中的[]

For example, age will only hold one data-entry. 例如, age将仅保存一个数据输入。 While age[] can hold multiple values. age[]可以容纳多个值。 If you want to add the element, just clone the said object, only if it doesn't have id with it. 如果要添加元素,只需克隆该对象(只有其没有id If you have multiple elements with same id, you can get unpredictable results. 如果您有多个具有相同ID的元素,那么您将获得无法预测的结果。 It's advisable to keep id's unique. 建议保持id唯一。

And on your PHP file, read the $_POST['name'] as an array. 然后在您的PHP文件上,将$_POST['name']读取为数组。

...... edited. ......编辑。

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

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