• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inserting multiple form fields with same name

#1
[eluser]Dede[/eluser]
Hi

This is my first post here, so please if I missed the correct forum thread, I'm sorry in advance.

I'm starting CI, and really like it, but have some trouble that cannot solve for few days.
I have a form with 4 fields in view:

Code:
<?php
  echo form_open('room_type/insert');
?>
<p>
  &lt;input type="button"  value="Add another room type" enabled="enabled" /&gt;
</p>
    <p><label for="room_types">Room type</label>&lt;input type="text" name="room_types[]" /&gt;&lt;/p>
    <p><label for="room_types_mk">Room type SR</label>&lt;input type="text" name="room_types_sr[]" /&gt;&lt;/p>
    <p><label for="max_occupancy">Max Occuancy</label>&lt;input type="text" name="max_occupancy[]" /&gt;&lt;/p>
    <p><label for="extra_bedding">Extra Bedding</label>&lt;input type="text" name="extra_bedding[]" /&gt;&lt;/p>
  


</div>
&lt;?php

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

           echo form_close();
        ?&gt;


As you can see, there's an "Add another room type" button on top of the form which lets me add the same form again under the first one, and the process can be repeated up to 5 forms, meaning I can insert (well, not exactly, since I haven't found a solution :redSmile 5 different room types with different occupancy, etc.
The other form fields are added with jQuery, and have the same names as the above posted.

In short, when I submit the form with e.g 3 sets of room types, I got the following multidimensional array:

Code:
Array
(
    [room_types] => Array
        (
            [1] => Single
            [2] => Double
            [3] => Three beds
        )

    [room_types_sr] => Array
        (
            [1] => Single SR
            [2] => Double SR
            [3] => Three beds SR
        )

    [max_occupancy] => Array
        (
            [1] => 1
            [2] => 2
            [3] => 3
        )

    [extra_bedding] => Array
        (
            [1] => 1
            [2] => 1
            [3] => 0
        )

    [insert_room_types] => Insert
)

I cannot find a way to sort the array in following format:
Code:
$insert_array = array(
  array('room_types' => 'Single', 'room_types_sr '=> 'Single SR' 'max_occupancy' => '1', 'extra_bedding' => '1'),
  array('room_types' => 'Double', 'room_types_sr '=> 'Double SR' 'max_occupancy' => '2', 'extra_bedding' => '1'),
            // etc....
);

);

Also, I'm using info from http://ellislab.com/codeigniter/user-gui...ysasfields and cannot validate appropriately.

What is the best practice to make DB inserts using AC for this type of form?

Thanks in advance


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

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.