Welcome Guest, Not a member yet? Register   Sign In
[kind of solved]Nested form_submit()
#1

[eluser]brucebat[/eluser]
Hi all,

I am using the form_helper in my view that deals with inputting data.
To make my form more adaptable to all users I have created a nested form_open within another form_open.

The purpose of this additional button is that when the user clicks it I want add another 5 rows of textboxes to the form so they can add more data if necessary.

Here is the code for the view:

Code:
echo form_open ('submit','submit/validate');

.
.
.
.
.

                if ( ! isset ($maxeventfields))
                $maxeventfields = 20;
                
                
                
                //prints 20 rows of textbox-textbox-textbox-pulldown-textbox.
                for ($i=0; $i< $maxeventfields; $i++)
                {
            
                    
                    //time part of event field
                    echo form_input ('time'.$i);
                    //the event itself
                    echo form_input ('event'.$i);
                    //supplies used
                    echo form_input ('supplies'.$i);
                    //successful?
                    echo form_dropdown ('success'.$i, $success);
                    //comment if necessary
                    echo form_input ('comment'.$i);
                    echo '<br/>';
                }
                
                echo form_open('submit/addeventsfields'); //calls function that adds more rows                echo form_submit ('addfields', 'Add rows');
                    echo form_hidden('addfields', '5');
                echo form_close();
                
                
              
                
                
                echo form_submit('submit', 'Submit Data');
                echo form_close();


Instead what is happening is it loads the function for processing the form instead of the one I want.

Any ideas how Im going to implement this?

Here is my controller code for adding the rows, it is definently not running as I have put a break in the function and echo to test it.

Code:
public function addeventsfields()
            {
                $maxeventfields = $maxeventfields + ($this->input->post('addfields'));
                echo $maxeventfields;
                break;
    
            }
#2

[eluser]brucebat[/eluser]
Still working on it, any ideas so I call the right function?
#3

[eluser]brucebat[/eluser]
Okay I have a problem in that when I add more rows I lose all of my form data that was entered.

Is there anyway to preserve it?

Thanks
#4

[eluser]TWP Marketing[/eluser]
You need separate forms in your view, since they do different actions. That means you have two functions in your controller, one to process the increment of maxeventfields and one to process the completed form.

VIEW CODE:
Code:
...
echo form_open ('submit','submit/validate'); // calls controller function which processes user input

if ( ! isset ($maxeventfields) )
  {
   $maxeventfields = 20; // THIS IS ONLY THE DEFAULT VALUE, IT MAY BE LARGER...
  }

  //prints SOME NUMBER OF rows of textbox-textbox-textbox-pulldown-textbox.
  for ($i=0; $i< $maxeventfields; $i++)
  {
   //time part of event field
   echo form_input ('time'.$i);
   //the event itself
   echo form_input ('event'.$i);
   //supplies used
   echo form_input ('supplies'.$i);
   //successful?
   echo form_dropdown ('success'.$i, $success);
    //comment if necessary
   echo form_input ('comment'.$i);
   echo '<br/>';
  }
echo form_submit('submit', 'Submit Data');
echo form_close(); // THIS CLOSES THE FIRST FORM
...
// NOW OPEN A SEPARATE FORM to request increment of maxeventfields
...
echo form_open('submit/addeventsfields'); //calls controller function that adds more rows
echo form_hidden('addfields', '5');
echo form_hidden('maxeventfields',$maxeventfields); // also pass the current value of maxeventfields back to the controller
echo form_submit ('addfields', 'Add rows');
echo form_close();  // THIS CLOSES THE SECOND FORM
...
Submit CONTROLLER CODE
Change Submit Controller function addeventfields() to process maxeventfields
Code:
...
public function addeventsfields( )
{
$maxeventfields = $this->input->post('maxeventfields'); // get current maxeventfields from form
$addfields      = $this->input->post('addfields'); // get increment value
if( $addfields )
{
  $maxeventfields = $maxeventfields + $addfields; // increment maxeventfields

//  echo $maxeventfields; // I Assume this line is just to TEST the value.  "Echo" does not belong in a controller function since it outputs directly to the browser.
}
...
// Now load the view again, with the possibly incremented value of maxeventfields.
$data['maxeventfields'] = $maxeventfields; // load maxeventfields into view data array
$this->load->view('your_view_name',$data); // RE-call the view with new maxeventfields value
}

public function validate()
{
...
// process the user input from first form
...
}
#5

[eluser]brucebat[/eluser]
Thanks for the code,

How would I preserve the values that are in my textboxes in the other form?

Example:

The user completes all the rows, then hits "Add more rows" and the page comes back but all the fields have been reset.

Any ideas how to solve that?

Thanks
#6

[eluser]TWP Marketing[/eluser]
[quote author="brucebat" date="1309724457"]Thanks for the code,

How would I preserve the values that are in my textboxes in the other form?

Example:

The user completes all the rows, then hits "Add more rows" and the page comes back but all the fields have been reset.

Any ideas how to solve that?

Thanks[/quote]

I haven't tested this, so the exercise is left to the student...:

Add them as hidden fields in the smaller form, so they will be passed to the controller.
The hidden field names should be exactly the same as the field names in the larger form.
Be sure to read the POSTed values in the controller.
You can then add them to the $data array and pass that back to your view.

I'm not sure this will work.
If someone else has input, please feel free to suggest it.

[EDIT]
On thinking about this problem, (passing the user input values back with an add-fields request included),
I don't think my above solution will work.

The other way is to use only one form (as your original code does) and read the submit buttons value.
You have two submit button on ONE form. You read the POSTed value for both buttons, One will be set and the other not set (false).
In your SINGLE controller function, you would have a conditional structure:
IF the "Add More Fields' submit button is valid,
1) increment the field count
2) read all the POSTed field,
3) load their values into the $data array and
3) call the view again.
ELSE
If the normal submit button is valid,
normal form entry processing would occur.

I'll work out valid code for this and post in a few minutes.
#7

[eluser]brucebat[/eluser]
How about for my other form I call the same function in the controller and have a hidden boolean value to act as a switch,

Use an if function to switch so

Code:
$addmorerows

if ($addmorerows == TRUE)

{ do the addition and return to form}

else

{proceed through to validation} ??

What do you think?
#8

[eluser]brucebat[/eluser]
Ok I tried it these two ways

1#-

I created the same array of hidden inputs with the same names and set value function to the original form.

Did not work!

2#-

I set the second form submit button to the same controller function and used a hidden form input with a boolean to set a swithc.

The function worked but because it was different form it kept losing the values when going back.


-SOLUTION-

Javascript .... I hope... well let you know how I get on.

Thanks for your input Smile
#9

[eluser]TWP Marketing[/eluser]
yes, that is the exact process, you can do that without my posting a whole lot of code then.
if it's an addition of fields request, remember to read the user input fields and load them into the $data array for the view.

I would not have used java here, but you can do that. It will keep your processing local, rather than on the server.




Theme © iAndrew 2016 - Forum software by © MyBB