简体   繁体   中英

Counting total number of POSTs in controller

A bit of background: I am building an transportation system for a school. The admin can add the places to configure the route. I have a just gave a text box to add a place and ADD button to keep adding more text boxes so he can add multiple places at once. The problem: The user fills the data, if he wish he adds as many as input boxes he needs and sends that to controller. How can i find out, how many he added the place. I can not use JavaScript Validation because of few reason. I am using php-codeigniter

<div class="control-group">
                <input type="hidden" name="count" value="1" />
                <div class="control-group" id="fields">
                    <label class="control-label" for="field1">Enter Place/Stop</label>
                    <div class="controls" id="profs"> 
                        <div class="input-append">
                            <input autocomplete="off" class="span10" id="field1" name="place_6" type="text" data-items="8"/><button id="b1" class="btn btn-info add-more" type="button">Add more</button>
                        </div>
                    <br>
                    <small>Press button to add another place</small>
                    </div>
                </div>
                </div>

And the jQuery

$(document).ready(function(){
var next = 1;
$(".add-more").click(function(e){
    e.preventDefault();
    var addto = "#field" + next;
    next = next + 1;
    var newIn = '<br /><br /><input autocomplete="off" class="span10" id="field' + next + '" name="place_'  + (next+5) + '" type="text">';
    var newInput = $(newIn);
    $(addto).after(newInput);
    $("#field" + next).attr('data-source',$(addto).attr('data-source'));
    $("#count").val(next);  
});
});

Make sure that new and existing inputs are given the name with an ending "[]" instead of a digit and you will get an array inside your $_POST. This:

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

Would give you:

$_POST['places']; # array() with all posted inputs

And from there you could just do a count() .

count($_POST['places']);

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