Welcome Guest, Not a member yet? Register   Sign In
Form validation for HTML Array
#1

[eluser]alwaystyred[/eluser]
Hi all, I am trying to create a HTML form that allows people to sign other people up. They will be able to click on a button that will, using JavaScript, add extra inputs to the form. The JavaScript code for this is:

Code:
var newdiv = document.createElement('div');
  //html for the extra form inputs
  newdiv[removed] = '<p><label for="title">Title <span class="required">*</span></label><br /><select name="title_array[]"><option value="" selected="selected">Please Select</option><option value="Mr">Mr</option><option value="Mrs">Mrs</option><option value="Miss">Miss</option><option value="Ms">Ms</option><option value="Dr">Dr</option></select></p><p><label for="first_name">First Name <span class="required">*</span></label><br />&lt;input id="first_name" type="text" name="first_name_array[]"  value=""  /&gt;&lt;/p><p><label for="last_name">Last Name <span class="required">*</span></label><br />&lt;input id="last_name" type="text" name="last_name_array[]"  value=""  /&gt;&lt;/p><p><label for="email_address">Email Address <span class="required">*</span></label><br />&lt;input id="email_address" type="text" name="email_address_array[]"  value=""  /&gt;&lt;/p></p>';
  console.log(number_of_added_fields);

  document.getElementById('contacts').appendChild(newdiv);

When the form is submitted, I am trying to validate each of the extra people that they have added by first counting the number of extra contacts, and then iterating through to validate each of them:

Code:
for($i = 0; $i = count($this->input->post('first_name_array')); $i++) {

//validate each of the extra contacts, one by one
$this->form_validation->set_rules('title_array', 'Title', 'required|trim|xss_clean');  
$this->form_validation->set_rules('first_name_array', 'First Name', 'required|trim|xss_clean');  
$this->form_validation->set_rules('last_name_array', 'Last Name', 'required|trim|xss_clean');  
$this->form_validation->set_rules('email_address_array', 'Email Address', 'required|trim|xss_clean|valid_email|is_unique[member_details.member_email_address]');  
  
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
  
}

The problem that I have is that
Code:
count($this->input->post('first_name_array'))
always returns 1, even if the extra inputs haven't been added to the form. For some reason this seems to either set off an infinite loop or make it hang. Basically, I click submit and nothing happens, the loading notification keeps on spinning.

I realise that I have probably made a real rookie mistake, but this is starting to stress me out a bit now. If anyone can offer any advice, I would be very grateful. If you want to see any more code, please let me know.

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB