Welcome Guest, Not a member yet? Register   Sign In
Need help With Submitting a Dynamic Form
#1

[eluser]vincej[/eluser]
Hi - I have a CI form which is populated with values from a DB by looping through the DB values using a foreach. It works fine. However, I want to be able to amend those values and then return the new values back to the DB.

The problem is that because the form is built with a foreach only the very last row of DB values can be amended. This is because the previous rows get overwritten as the foreach loops through.

Question: what is the mechanism I need to use to update the values from the whole form ??

Here is the code as it exists at the moment:

Model:

Code:
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);

}



Controller:

Code:
function UpdateLocation(){
$this->MPickup->UpdateLocation();
redirect('admin/pickup_detail','refresh');
}


View:

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

<table width="800" border="30" cellpadding="5" align="center"  >
  <tr>
   &lt;!-- <th scope="col">Pick Up ID</th>--&gt;
    <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>

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

</table>

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

[eluser]jjDeveloper[/eluser]
You should check out the form helper and form validation class as I beleive you will find a wealth of information there to help you.
#3

[eluser]vincej[/eluser]
Thanks for that - however, I've been studying the user guide avidly and I could be blind but I don't see the answer there.

what I think I need to do is create an array out of each of my form input names ie

Code:
<tr align="center">
    <td>&lt;?php $data = array('name'=>'pickuplocation[]','id'=>$row['pickupid'],'size'=>20,'value' => $row['pickuplocation']); echo form_input($data); ?&gt;</td>
    <td>&lt;?php $data = array('name'=>'Date1[]','id'=>$row['Date1'],'size'=>20,'value' =>$row['Date1']); echo form_input($data); ?&gt;</td>
    <td>&lt;?php $data = array('name'=>'Date2[]','id'=>$row['Date2'],'size'=>20,'value' => $row['Date2']); echo form_input($data); ?&gt;</td>
    <td>&lt;?php $data = array('name'=>'Date3[]','id'=>$row['Date3'],'size'=>20,'value' =>  $row['Date3']); echo form_input($data); ?&gt;</td>
  </tr>

But for some reason it is not working - ugh
#4

[eluser]jjDeveloper[/eluser]
The reason I mentioned this is have a look at the set_value function in this form taken from the user_guide. If you pass an array to your view you could pre-set the values and then when you submit you have the new values.

Unless I am misunderstanding your question.
Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;My Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php echo validation_errors(); ?&gt;

&lt;?php echo form_open('form'); ?&gt;

<h5>Username</h5>
&lt;input type="text" name="username" value="&lt;?php echo set_value('username'); ?&gt;" size="50" /&gt;

<h5>Password</h5>
&lt;input type="text" name="password" value="&lt;?php echo set_value('password'); ?&gt;" size="50" /&gt;

<h5>Password Confirm</h5>
&lt;input type="text" name="passconf" value="&lt;?php echo set_value('passconf'); ?&gt;" size="50" /&gt;

<h5>Email Address</h5>
&lt;input type="text" name="email" value="&lt;?php echo set_value('email'); ?&gt;" size="50" /&gt;

<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
#5

[eluser]vincej[/eluser]
Thnaks jj - I've used the set value before - unfortunately it is not the problem I am having. My problem is in submitting the values of a form when those values are generated through a foreach loop. Essentially what happens is as the foreach cycles through it overwrites the previous value. The result is an HTML for with all the values in the DOM but not int he $variable.

The solution I am finding is to convert each of the form names to an array thus []. This way, as the foreach cycles through the name array will capture all the values which can then be submitted as an array.

I still don't have it all working yet, but I am making some progress.

#6

[eluser]jjDeveloper[/eluser]
according to the user guide the following should work.
Code:
<tr align="center">
    <td>&lt;?php $data = array('name'=>'pickuplocation','id'=>$row['pickupid'],'size'=>20,'value' => $row['pickuplocation']); echo form_input($data); ?&gt;</td>
    <td>&lt;?php $data = array('name'=>'Date1','id'=>$row['Date1'],'size'=>20,'value' => set_value('Date1', $row['Date1']); echo form_input($data); ?&gt;</td>
    <td>&lt;?php $data = array('name'=>'Date2','id'=>$row['Date2'],'size'=>20,'value' => set_value('Date2', $row['Date2']); echo form_input($data) ; ?&gt;</td>
    <td>&lt;?php $data = array('name'=>'Date3','id'=>$row['Date3'],'size'=>20,'value' =>  set_value('Date3', $row['Date3']); echo form_input($data ); ?&gt;</td>
  </tr>



Here is a form I use but there is no default value set like i applied to your situation.

Code:
<div id="registrationForm">
  <h2>&lt;?php echo $pageTitle; ?&gt;</h2>
&lt;?php
  echo validation_errors();
  echo form_open($formAction);
  echo form_label('First Name: ', 'firstName');
  echo form_input('firstName', set_value('firstName'));
  echo br(1);
  echo form_label('Last Name: ', 'lastName');
  echo form_input('lastName', set_value('lastName'));
  echo br(1);
  echo form_label('Address: ', 'address');
  echo form_input('address', set_value('address'));
  echo br(1);
  echo form_label('City: ', 'city');
  echo form_input('city', set_value('city'));
  echo br(1);
  echo form_label('Postal Code: ', 'pc');
  echo form_input('pc', set_value('pc'));
  echo br(1);
  echo form_label('Email: ', 'email');
  echo form_input('emailAddress', set_value('emailAddress'));
  echo br(1);
  echo form_label('Confirm E-mail: ', 'emailConfirm');
  echo form_input('confirmEmail', set_value('confirmEmail') );
  echo br(1);
  echo form_label('Phone Number: ', 'phoneNumber');
  echo form_input('phoneNumber', set_value('phoneNumber'));
  echo br(1);
  echo form_label('Password: ', 'password');
  echo form_password('password', '');
  echo br(1);
  echo form_label('Confirm Password: ', 'confirm_password');
  echo form_password('confpassword', '');
  echo br(1);
  echo form_submit('submit', 'Submit Registration');
  echo form_close();
?&gt;
</div>
#7

[eluser]Unknown[/eluser]
Code:
&lt;?php foreach ($query->result_array() as $row) { ?&gt;
<tr align="center">
    <td>&lt;?php $data = array('name'=>'pickuplocation['.$row['pickupid'].']', 'size'=>20,'value' => $row['pickuplocation']); echo form_input($data); ?&gt;</td>
    <td>&lt;?php $data = array('name'=>'Date1['.$row['pickupid'].']','size'=>20,'value' =>$row['Date1']); echo form_input($data); ?&gt;</td>
    <td>&lt;?php $data = array('name'=>'Date2['.$row['pickupid'].']','size'=>20,'value' => $row['Date2']); echo form_input($data); ?&gt;</td>
    <td>&lt;?php $data = array('name'=>'Date3['.$row['pickupid'].']','size'=>20,'value' =>  $row['Date3']); echo form_input($data); ?&gt;</td>
  </tr>
&lt;?php } ?&gt;
Code:
&lt;?php
function Updatelocation(){

if( isset( $_POST['pickuplocation'] ) ) {
  foreach( $_POST['pickuplocation'] as $pickupid=>$pickuplocation ) {
   $data = array(
    'pickuplocation' => db_clean($pickuplocation),
    'Date1' => db_clean($_POST['Date1'][$pickupid]),
    'Date2' => db_clean($_POST['Date2'][$pickupid]),
    'Date3' => db_clean($_POST['Date3'][$pickupid]),
   );
   $this->db->where('pickupid', $pickupid);
   $this->db->update('pickup',$data);

}
?&gt;
#8

[eluser]vincej[/eluser]
You Are MEGA !!!!!!!

THANK YOU Very much !! I would have never got there without you !

For someone who has 'never been on the site' - you are pretty amazing - I reckon there is a story behind you are ! Big Grin

vincej




Theme © iAndrew 2016 - Forum software by © MyBB