简体   繁体   English

如何检查填写的字段数?

[英]How to check number of fields filled out?

i am stuck in this small issue: 我陷入了这个小问题:

i have a page where user can fill out up to 10 fields. 我有一个页面,用户可以填写多达10个字段。 but he doesnot have to, he can also fill out only one. 但他不必这样做,他也只能填写一个。 at first, user has only one field, then he can add more fields with add button. 首先,用户只有一个字段,然后可以使用add按钮添加更多字段。

my problem is: how can i know how many fields the user has added and filled out, so that i can save them into db? 我的问题是:我怎么知道用户已添加并填写了多少字段,以便可以将它们保存到数据库中?

my vision is this: 我的愿景是:

i will count the fields and then i pass this sum to serverside code. 我将对字段进行计数,然后将此总和传递给服务器端代码。 then there, i will loop sum-times and ....? 然后在那里,我将循环求和和....? i am stuck here, :( 我被困在这里,:(

can someone suggest me pls simpler logic, 有人可以建议我请更简单的逻辑,

thanks a lot 非常感谢

You can utilize the array-functionality of HTML-Elements. 您可以利用HTML元素的数组功能。

Have all fields have a name like this: 让所有字段都具有这样的name

<input type="text" name="Set[]"></input>

In this case, your $_POST -array will have an array called Set , which you can just iterate through. 在这种情况下, $_POST -array将具有一个名为Set的数组,您可以对其进行迭代。 Discard those, where empty() returns true and you're all set. 丢弃那些,其中empty()返回true并且您已全部设置。

Edit: Replaced fixed indices with automatic increment. 编辑:用自动增量替换固定索引。

A better idea to do this could be: 一个更好的主意是:

When you click on add button to add another textfield, a save button is displayed along with it and save the value instantly using jquery ajax. 当您单击添加按钮以添加另一个文本字段时,将显示一个保存按钮,并使用jquery ajax立即保存该值。

var numberoffields = 0;
addfield(){
//your code to add field here
numberoffields += 1;
}

and post 'numberoffields' to your server 并将“ numberoffields”发布到您的服务器

Try this server-side code: 试试这个服务器端代码:

<?php
    $vals = array();
    foreach($_POST as $key=>$val) {
        if(!empty($val)) {
            $vals[] = $val;
        } 
    }
?>

Use $vals . 使用$vals you're done. 你完成了。

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

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