I am building a website, where on the click of a link you can clone the form elements, what I am wanting to know is that, when I send the $_POST
to my controller and check that the information submitted is correct, how do I then return a template that has enough elements so that errors can be rectified, so example my original form looks like this,
<fieldset class="entry">
<label for="email_address">Email Address</label>
<input type="text" name="email_address[]" value="" class="text small"/>
<label for="firstname">Firstname</label>
<input type="text" name="firstname[]" value="" class="text small"/>
<label for="surname">Surname</label> <input type="text" name="surname[]" value="" class="text small"/>
</fieldset>
how can I return the correct amound of fieldsets based on the $_POST?
To answer this:
How can I return the correct amount of fieldsets based on the $_POST?
If each fieldset only has one instance of each bracketed[] field name, you can just count how many were submitted (of any of the fields).
$number_of_fieldsets = count((array) $this->input->post('email_address'));
I've used $this->input->post()
(since you're using CI) in case the value is not set (it will return false), you may use some isset()
logic instead if you wish.
I've cast to array here in case the return value of $this->input->post('email_address')
is false (count will return 0) or for some reason, a string (count will return 1). This is just a mild attempt at being defensive, you will probably want to handle unexpected results with your own methods.
Once again, it doesn't matter which field you choose to count.