Welcome Guest, Not a member yet? Register   Sign In
Inserting multiple form fields with same name
#6

[eluser]Dede[/eluser]
Guys, thank you for replies.

I've finally solved this with your help (I love this community).
The solution is kind of combination of all your proposed codes above.

Here's the controller method:
Code:
<?php
        protected $validate;

public function insert()
{
  
  $pointer_array = $this->input->post('pointer');
  
  // Grab the last value of the hidden field `pointer`
  $pointer = array_pop($pointer_array);
  
  // Loop and make validation rules on the fly
  for($i=1; $i<=$pointer; $i++){
   $this->validate = array(
    array(
     'field' => 'room_info['.$i.'][room_type]',
     'label' => 'Room type No.'. $i,
     'rules' => 'trim|required|xss_clean|min_length[3]'  
    ),
    array(
     'field' => 'room_info['.$i.'][room_type_mk]',
     'label' => 'Room Type MK No.'. $i,
     'rules' => 'trim|required|xss_clean|min_length[3]'
    ),
    array(
     'field' => 'room_info['.$i.'][max_occupancy]',
     'label' => 'Maximum Occupancy No.'. $i,
     'rules' => 'trim|required|xss_clean|min_length[1]'  
    ),
    array(
     'field' => 'room_info['.$i.'][extra_bedding]',
     'label' => 'Extra Bedding No.'. $i,
     'rules' => 'trim|xss_clean|min_length[1]'
    )  
   );
  }
  
  $this->load->library('form_validation');
  $this->form_validation->set_error_delimiters('<div class="alert-message error">', '</div>');
  $this->form_validation->set_rules($this->validate);
  
  if($this->form_validation->run() == true){

   $room_info = $this->input->post('room_info');
      
   foreach($room_info as &$info){    // Pass by reference
       //Add property ID to every set of the $room_info array
    array_unshift($info,$this->the_property_id);
    
    // Rename the key of "[0] => $this->the_property_id"
    // to  "[property_id] => $this->the_property_id" of the $room_info array
    $info['property_id'] = $info[0];
    unset($info[0]);
   }

  
   // Make bulk insert using Rumbelow’s MY_Model  
   $this->room_info->insert_many($room_info);
  
  
  
  
  }else {
   // TESTING ONLY !!!!!!
   print_r(validation_errors());
  }
          }
?&gt;

The $pointer variable is a hidden field dynamically added to each set of form fields through jQuery.

And the view:
Code:
&lt;?php
  echo form_open('room_type/insert');
?&gt;
<p>
  &lt;input type="button" id="add"  value="Add another room type" enabled="enabled" /&gt;
</p>
    <p><label>Room type</label>&lt;input type="text" name="room_info[1][room_type]" id="room_type" /&gt;&lt;/p>
    <p><label>Room type MK</label>&lt;input type="text" name="room_info[1][room_type_mk]" id="room_type_mk" /&gt;&lt;/p>
    <p><label>Max Occuancy</label>&lt;input type="text" name="room_info[1][max_occupancy]" id="max_occupancy" /&gt;&lt;/p>
    <p><label>Extra Bedding</label>&lt;input type="text" name="room_info[1][extra_bedding]" id="extra_bedding" /&gt;&lt;/p>
    &lt;input  type="hidden" name="pointer[]" value="1" /&gt;


</div>
&lt;?php

    $submit_attr = array(
       'type' =>'submit',
       'name'=>'insert_room_types'
       );
    
    echo form_submit($submit_attr, 'Insert');

           echo form_close();
        ?&gt;

The jQuery part:
Code:
$(function() {
var room_div = '#room_type_form';
$('#add').attr('enabled', 'enabled')

var i = 1;

$('#add').live('click', function() {
  i++;
  var open_fieldset = '<div class="the_parent"><fieldset><legend>Room Type ' + i + '</legend>'
       + '<p><label for="room_types">Room type</label>&lt;input type="text" name="room_info[' + i + '][room_type]" id="room_type" /&gt;&lt;/p>'
        + '<p><label for="room_types_mk">Room type MK</label>&lt;input type="text" name="room_info[' + i + '][room_type_mk]" id="room_type_mk" /&gt;&lt;/p>'
        + '<p><label for="max_occupancy">Max Occuancy</label>&lt;input type="text" name="room_info[' + i + '][max_occupancy]" id="room_type_mk" /&gt;&lt;/p>'
        + '<p><label for="extra_bedding">Extra Bedding</label>&lt;input type="text" name="room_info[' + i + '][extra_bedding]" id="room_type_mk" /&gt;&lt;/p>'
        + '<p><a href="#" id="remove_fieldset" class="btn small danger">Remove</a></p>'
        + '</fieldset>'
        + '</div>'
        + '&lt;input type="hidden" name="pointer[]" value="' + i + '" /&gt;"';

  $(open_fieldset).appendTo(room_div);
  

  if( i >= 5 ){ // max 5 input fields
   $('#add').attr('disabled', 'disabled').val('Maximum No. of rom types reached');
  }
});
$('#remove_fieldset').live('click', function(){
  /*if( i > 2 ) {*/
                $(this).parents('div.the_parent').remove();
                i--;    
  /*}*/
  if( i < 6 ){
     $('#add').removeAttr("disabled").val('Add another Room Type');
  }
  
        return false;
});
});

Note that the JavaScript part above is for 5 room type sets max.

On the other hand, on validation arrays, I just saw this and it is fresh: https://github.com/EllisLab/CodeIgniter/pull/1105. I guess it will be merged into the core after testing.


Messages In This Thread
Inserting multiple form fields with same name - by El Forum - 03-01-2012, 07:05 PM
Inserting multiple form fields with same name - by El Forum - 03-01-2012, 11:46 PM
Inserting multiple form fields with same name - by El Forum - 03-02-2012, 08:41 AM
Inserting multiple form fields with same name - by El Forum - 03-02-2012, 08:50 AM
Inserting multiple form fields with same name - by El Forum - 03-02-2012, 11:20 AM
Inserting multiple form fields with same name - by El Forum - 03-02-2012, 09:44 PM



Theme © iAndrew 2016 - Forum software by © MyBB