简体   繁体   English

处理动态生成的表单中的数据

[英]Handling datas from dynamically generated form

I thought of making something that generates lots of form dynamically and then using jquery to handle their submission. 我想到制作一些可以动态生成大量表单的东西,然后使用jquery处理其提交。

So i generate the form using php by using something like this. 所以我通过使用类似这样的东西使用php生成表单。

while(some condition) {
    echo "<form method=post action=specific_url.php name=some_form_name>";
    echo "<input type=text>";
    echo "<input type=submit>";
    echo "</form>";
}

Well the above is just a skeleton. 好吧,以上只是一个框架。 The main Problem is how should i name/id the form elements so that i can uniquely identify each form during submission and submit them, and use can extract the data at the specific_url page without any problem. 主要问题是我应该如何命名/标识表单元素,以便我可以在提交过程中唯一地标识每个表单并提交它们,并且使用该表单可以提取specific_url页面上的数据而没有任何问题。

A direct analogy of this feature that i can think at the top of my head is that of facebook posts, where each post has got a comment box. 我可以想到的最类似于此功能的是Facebook帖子,每个帖子都有一个评论框。

UPDATE 更新

I guess i wasn't very clear with the question. 我想我对这个问题不太清楚。 Sorry about that. 对于那个很抱歉。 Let me rephrase it. 让我改一下。

So till now, I have these dynamically generated forms. 因此,到目前为止,我拥有这些动态生成的表格。 Now suppose user writes in one of the textbox and click on submit button. 现在,假设用户在其中一个文本框中写入内容,然后单击“提交”按钮。

At these point jquery should take control of it. 此时,jquery应该控制它。 Send that particular form data to the url. 将特定的表单数据发送到url。 Retrieve the result and do something. 检索结果并执行某些操作。

So in the end, I should be able to do something like this 所以最后,我应该可以做这样的事情

$(some_selector).submit(function(e) {
    e.preventDefault();
    $.post('specific_url.php',$(some_selector).serialize(),function(data) {
        // some stuff
    });
});

Here "some_selector" is that selector which gets activated when a particular form is submitted and then sending that form's data. 此处的“ some_selector”是在提交特定表单然后发送该表单的数据时激活的选择器。

You can use a hidden form element and set its value to an id for the form. 您可以使用隐藏的表单元素并将其值设置为表单的ID。

<input type="hidden" name="form_id" value="1">

This way you can access the form_id like any other form value once you submit it. 这样,您可以像提交任何其他表单值一样访问form_id。 I am looking at the facebook source now and I see a few hidden form elements next to the comment boxes 我现在正在查看facebook源,并且在注释框旁边看到一些隐藏的表单元素

It is a requirement that the element id attribute is unique, not so with the name attribute. 要求元素id属性必须唯一,而name属性则必须唯一。

It is also possible to use arrays of elements, for example : 也可以使用元素数组,例如:

<input name="comment[]" type=text>
<input name="comment[]" type=text>
<input name="comment[]" type=text>.

In your PHP. 在您的PHP中。 the value of $_POST['comment'] will be send as an array. $ _POST ['comment']的值将作为数组发送。

If you want to uniquely identify each comment in your PHP code, you will be forced to rename them uniquely. 如果要在PHP代码中唯一标识每个注释,则将被迫对其进行唯一重命名。

<input name="comment1" type=text>
<input name="comment2" type=text>
<input name="comment3" type=text>

Since you are generating the form using PHP, this would be easy to keep track of the name that should be generated. 由于您是使用PHP生成表单的,因此很容易跟踪应生成的名称。

If you have multiple forms, you have free reign, sort of. 如果您有多种形式,则可以自由统治。 There is no need a name for the form. 表单不需要名称。 However, to identify where in the page the form is places, place a hidden field. 但是,要确定表单在页面中的位置,请放置一个隐藏字段。

while(some condition) {
    echo "<form method=post action=specific_url.php";
    echo "<input name='position_in_page' type='hidden' value='$somevalue'>";
    echo "<input name='comment' type='text'>";
    echo "<input type='submit'>";
    echo "</form>";
}

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

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