Jump to content

Need help With Submitting a Dynamic Form


vincej

Recommended Posts

Hi - I have a form which is populated with values from a DB. This is done by looping through the DB values using a foreach. It works fine. I populate my form with those values. However, I want to be able to amend those values, and then submit the new values back to the DB with 1 single submit button. I don't want a separate submit button for each row of my form.

 

The problem is that because the form is built with a foreach, as the it loops through the variables on each pass of the DB, only the final row of DB are present in the form variables.

 

Question: My 'foreach' approach must be faulty. What is the mechanism or approach I need to use to update the values from the whole form  ??

 

MANY THANKS for all your help !!

Link to comment
Share on other sites

sure - I can give you my code, no problem, but it is built on CodeIgniter - a PHP Object Orientated MVC framework. CI is just PHP  but I assumed that it would not be very meaning full to many people.  Unfortunately no one on the CI site knows the answer.  I'm starting to believe that maybe I need to use JQuery. Anyway Here is the code:

 

Web Form ie the View:

 

<?php
echo form_open('admin/pickup_detail/Updatelocation');
?>

<table width="800" border="30" cellpadding="5" align="center"  >
  <tr>
   <!-- <th scope="col">Pick Up ID</th>-->
    <th width="147" scope="col">Pick Up Location</th>
    <th width="98" scope="col">Date 1 </th>
    <th width="98" scope="col">Date 2 </th>
    <th width="98" scope="col">Date 3</th>

<?php 
foreach ($query->result_array() as $row) {
?>
<tr align="center">
    <td><?php $data = array('name'=>'pickuplocation','id'=>$row['pickupid'],'size'=>20,'value' => $row['pickuplocation']); echo form_input($data); ?></td>
    <td><?php $data = array('name'=>'Date1','id'=>$row['Date1'],'size'=>20,'value' =>$row['Date1']); echo form_input($data); ?></td>
    <td><?php $data = array('name'=>'Date2','id'=>$row['Date2'],'size'=>20,'value' => $row['Date2']); echo form_input($data); ?></td>
    <td><?php $data = array('name'=>'Date3','id'=>$row['Date3'],'size'=>20,'value' =>  $row['Date3']); echo form_input($data); ?></td>
  </tr>
<?php } ?>

</table>

<?php
echo "<div class=submitted>". form_submit('submit','Save Changes')."</div>";
echo form_close();
?> 

 

 

Controller:

 

<?php function UpdateLocation(){
$this->MPickup->UpdateLocation();
redirect('admin/pickup_detail','refresh');
}?> 

 

 

DataBase Access ie the Model:

 

<?php function Updatelocation(){
$data = array( 'pickuplocation' => db_clean($_POST['pickuplocation']),
                         'Date1' => db_clean($_POST['Date1']),
      'Date2' => db_clean($_POST['Date2']),
     'Date3' => db_clean($_POST['Date3']),
     
     );

$this->db->where('pickuplocation', $_POST['pickuplocation']);
$this->db->update('pickup',$data); 

}?> 

 

 

 

Link to comment
Share on other sites

Since each form field name is the same, you will only get the last row's data in the submitted form data (as you mentioned).  You'll need to indicate that the form data should be handled as an array by adding [] to each form field name:

 

<input type="text" name="pickuplocation[]">

 

You might also consider including the pickupid as the array key (assuming it's your DB table's primary key).  It's not really necessary but it might help you process the data more easily:

 

<input type="text" name="pickuplocation[<?php echo $row['pickupid']; ?>]">

 

PHP will then process the form values into an array that you can loop through:

 

<?php
$pickuplocations = $this->input->post('pickuplocation');
foreach ($pickuplocations as $key => $location) {
    echo "The pickup ID is $key and the location is $location";
}
?>

 

Then once you have captured and organised each row's data (probably into a nested array with pickupids as the keys), you would need to run the DB update code in a loop:

 

<?php
foreach ($organised_data as $data) {
    $this->db->where('pickupid', $data['pickupid']);
    $this->db->update('pickup', $data); 
}
?>

 

Hope this makes sense...

Link to comment
Share on other sites

Hi  behicthebuilder - I get the feeling you know CI. I have followed your advice and this has really made a difference - I'm 90% the way there but I'm stumbling on the DB update ( model) This is what I've got:

 

<?php function Updatelocation(){

$organised_data = array(
'pickupid' => $this->input->post('pickupid'),
'pickuplocations' => $this->input->post('pickuplocation'),
'Date1' => $this->input->post('Date1'),
'Date2' => $this->input->post('Date2'),
'Date3' => $this->input->post('Date3'),
);

foreach ($organised_data as $key=>$data) {
    $this->db->where('pickupid', $organised_data['pickupid']);
    $this->db->update('pickup', $data); 
}?>

 

if you do PRINT_R  on the $organised_data you get a very nice nested array of all the values, but when I go to up date the DB my code is not reading the array down the layers, so looking at the CI Profiler for my last query I get:

 

 DATABASE:  countrywide   QUERIES: 5  (Hide)
0.0000  	UPDATE `pickup` SET `0` = '1', `1` = '2', `2` = '3', `3` = '4' WHERE `pickupid` =  Array 
0.0000  	UPDATE `pickup` SET `0` = 'collingwood', `1` = 'varsity', `2` = 'canmore', `3` = 'westbrook' WHERE `pickupid` =  Array 
0.0000  	UPDATE `pickup` SET `0` = '1327359441', `1` = '1327359441', `2` = '1', `3` = '1237' WHERE `pickupid` =  Array 
0.0000  	UPDATE `pickup` SET `0` = '1327359442', `1` = '1327359442', `2` = '2', `3` = '566' WHERE `pickupid` =  Array 
0.0000  	UPDATE `pickup` SET `0` = '1327359443', `1` = '1327359443', `2` = '3', `3` = '987' WHERE `pickupid` =  Array  

 

Clearly I am not not good at traversing nested arrays !

 

Thank you very much rfor any help you might be able to offer.

 

 

Link to comment
Share on other sites

The problem here is that the $organised_data variable needs to be an array of associative arrays (where each associative array represents a row of data to be updated in your database).  At the moment, you have organised the submitted data in such a way that there is no relationship between values. 

 

You have an array that contains:

 

1. An array of pickupids

2. An array of pickuplocations

3. An array of Date1s

4. An array of Date2s

5. An array of Date3s

 

When what you need is an array of arrays that look something like:

 

<?php
// I'm assuming the pickupid shouldn't be included in the data to be updated (since you used it as your WHERE clause)
$single_row = array(	
'pickuplocation' => 'Something',
'Date1' => 'some date',
'Date2' => 'another date',
'Date3' => 'different date'
);
?>

 

This is also why I suggested using the pickupid (or whatever your table's primary key is) as your array keys in your HTML form.  This would make it easier to manage the submitted data and maintain relationships between the data for updating.

Link to comment
Share on other sites

Hi Thanks for  all your help.

 

I tried putting the pickupid into the statement with single and double quotes where you suggested but CI gave me an error "Disallowed keys".

 

td><?php $data = array('name'=>'pickuplocation[$row[pickupid]]','id'=>$row['pickupid'],'size'=>20,'value' => $row['pickuplocation']); echo form_input($data); ?></td>

 

 

I understand what you are saying about a single row, but without the pickupid - I don't know how to get there.

 

sorry I am being a bit dumb on this.

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.