CodeIgniter Forums
How To Add Many Identical Fields Into a Form for Submission - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How To Add Many Identical Fields Into a Form for Submission (/showthread.php?tid=50327)



How To Add Many Identical Fields Into a Form for Submission - El Forum - 03-22-2012

[eluser]vincej[/eluser]
Hi - I have a simple HTML form which presents on each row a location with 4 dates.

The array and foreach loop which generates the HTML appears to be correct. I.e. if I look into the HTML source I see every location with every date including the vital hidden data which uniquely identifies each specific date.


My problem appears to be that because each row in the source HTML has the same name (see source below), when I do a 'submit', the POST array gets overwritten and hence only the very last row is now delivered into my model.

HTML Source extract:

Code:
<td width="100" align="center">4
&lt;input type="hidden" name="locationid" value="4" /&gt;
</td>
   <td width="100" align="center">&lt;input type="text" name="location" value="Collingwood" size="20"  /&gt;&lt;/td>
   <td width="100">&lt;input type="text" name="1293704800" value="30 Dec 2010" size="15" theIDs="2"  /&gt;&lt;/td>
   <td width="100">&lt;input type="text" name="1311704800" value="26 Jul 2011" size="15" theIDs="1"  /&gt;&lt;/td>
   <td width="100">&lt;input type="text" name="1315704800" value="10 Sep 2011" size="15" theIDs="3"  /&gt;&lt;/td>
   <td width="100">&lt;input type="text" name="1337704800" value="22 May 2012" size="15" theIDs="4"  /&gt;&lt;/td>
   <td width="150"> <a href="http://localhost/countrylane/index.php/admin/pickup_detail/deletelocation/Collingwood">Delete</a></td>
  
</tr>
     <tr align="center">
<td width="100" align="center">5
&lt;input type="hidden" name="locationid" value="5" /&gt;
</td>
   <td width="100" align="center">&lt;input type="text" name="location" value="Varsity" size="20"  /&gt;&lt;/td>
   <td width="100">&lt;input type="text" name="1331704800" value="14 Mar 2012" size="15" theIDs="5"  /&gt;&lt;/td>
   <td width="100">&lt;input type="text" name="1334704800" value="17 Apr 2012" size="15" theIDs="6"  /&gt;&lt;/td>
   <td width="100">&lt;input type="text" name="1337704800" value="22 May 2012" size="15" theIDs="7"  /&gt;&lt;/td>
   <td width="100">&lt;input type="text" name="1339704800" value="14 Jun 2012" size="15" theIDs="8"  /&gt;&lt;/td>
   <td width="150"> <a href="http://localhost/countrylane/index.php/admin/pickup_detail/deletelocation/Varsity">Delete</a></td>
  
</tr>


My second problem is that 'theIDs' are all being ignored by my model.

I expect that possibly I should rework my model to do multiple DB updates or do a Batch Update ???


Model:

Code:
function Updatelocation(){
  $locationid = $_POST['locationid'];  
  $data = array(
  'locationid' => db_clean($locationid),
  'puid' => db_clean($_POST['theIDs[0]']),
  'puid' => db_clean($_POST['theIDs[1]']),
  'puid' => db_clean($_POST['theIDs[2]']),
  'puid' => db_clean($_POST['theIDs[3]']),
  'dates' => db_clean(strtotime($_POST[$theDates[0]])),
  'dates' => db_clean(strtotime($_POST[$theDates[1]])),
  'dates' => db_clean(strtotime($_POST[$theDates[2]])),
  'dates' => db_clean(strtotime($_POST[$theDates[3]]))
    );
   $this->db->where('locationid', $locationid);
    $this->db->update('pudates',$data);
}


sorry if all this sounds a bit of a noob question,

Many Many Thanks Vincej


How To Add Many Identical Fields Into a Form for Submission - El Forum - 03-22-2012

[eluser]rwestergren[/eluser]
To have an array with the same name posted to your controller, name your inputs like this:

Code:
<td width="100" align="center">&lt;input type="text" name="location[]" value="Collingwood" size="20"  /&gt;&lt;/td>

Notice the "name." Also, you shouldn't read your post values directly in your model. Rather, you'd want to process them in the controller and send them to the model as a parameter.


How To Add Many Identical Fields Into a Form for Submission - El Forum - 03-22-2012

[eluser]vincej[/eluser]
HI Thanks you Very much for your help !

I'm still doing something wrong as when adding the array to locations[] the POST array now looks like this:

Code:
Array
(
    [locationid] => Array
        (
            [0] => 4
            [1] => 5
        )

    [location] => Varsity
    [1293704800] => 30 Dec 2010
    [1311704800] => 26 Jul 2011
    [1315704800] => 10 Sep 2011
    [1337704800] => 22 May 2012
    [1331704800] => 14 Mar 2012
    [1334704800] => 17 Apr 2012
    [1339704800] => 14 Jun 2012
    [submit] => Save Changes
)
what I am trying to do is get a POST array which has the locationid but MOST importantly those valuable 'theID's' which uniquely identifies each individual date - critical for amending the dates in the DB. 'theIDs' are being completely ignored by SUBMIT and my POST

I do have a controller but it is just 1 line so I do not bother to present it here.

Many Thanks !






How To Add Many Identical Fields Into a Form for Submission - El Forum - 03-22-2012

[eluser]vincej[/eluser]
OK - I have the ID's coming in, in the correct spot - my bad !

Code:
<tr align="center">
<td width="100" align="center">&lt;?php echo  $item['locationid']. form_hidden('locationid[]', $item['locationid']);?&gt;</td>
   <td width="100" align="center">&lt;?php $data = array('name'=>'location', 'size'=>20,'value' => $item['location']); echo form_input($data); ?&gt;</td>
   <td width="100">&lt;?php $data = array('name'=> $theIDs[0],'size'=>15,'value' =>(strftime("%d %b %Y",$theDates[0]))); echo form_input($data);?&gt;</td>
   <td width="100">&lt;?php $data = array('name'=> $theIDs[1],'size'=>15,'value' =>(strftime("%d %b %Y",$theDates[1]))); echo form_input($data);?&gt;</td>
   <td width="100">&lt;?php $data = array('name'=> $theIDs[2],'size'=>15,'value' => (strftime("%d %b %Y",$theDates[2]))); echo form_input($data); ?&gt;</td>
   <td width="100">&lt;?php $data = array('name'=> $theIDs[3],'size'=>15,'value' => (strftime("%d %b %Y",$theDates[3]))); echo form_input($data);?&gt;</td>
   <td width="150"> &lt;?php echo anchor('admin/pickup_detail/deletelocation/'. $item['location'], 'Delete');?&gt;</td>
  
</tr>



Now I have to fix my model.