简体   繁体   中英

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. 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.

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.

Have all fields have a name like this:

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

In this case, your $_POST -array will have an array called Set , which you can just iterate through. Discard those, where empty() returns true and you're all set.

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.

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

and post 'numberoffields' to your server

Try this server-side code:

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

Use $vals . you're done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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