Welcome Guest, Not a member yet? Register   Sign In
Split form in many pages... better way?
#11

[eluser]Aken[/eluser]
[quote author="CroNiX" date="1341255594"]If you are going to store the previous data in hidden fields, wouldn't that mean you are going to constantly be revalidating the same data, and an ever increasing amount for each step that you progress through?[/quote]

+1 - Do NOT use hidden fields, they can be manipulated. Don't trust your users.
#12

[eluser]Lewis Cowles[/eluser]
you could have the entire form on a single page inside a div with overflow: hidden; in the css and use
Code:
<a name="section1">...
<a name="section2">...
this will obviously have an effect loading times, but it will allow for a smoother transition between elements.

Also check out how Magento does this; It's the same thing, but with jQuery animations to toggle between fieldsets, for that little extra visual flair. very very nice to be able to add this kind of functionality.

Also as a N.B. consider that the form should be external to the div's
Code:
&lt;form ...&gt;&lt;div ...>content...</div>&lt;/form&gt;
, this will ensure that, the form as a whole can be saved. If you use localstorage, you can also save the form data on the users machine without having to worry about filling a database or storing a cookie (which might not be large enough, depending upon the size of the form.
#13

[eluser]henry178[/eluser]
the "hidden fields" must be controlled in every controller; because these could be manipulate.

How can I do?
#14

[eluser]henry178[/eluser]
ps I have also image fileds.
#15

[eluser]cartalot[/eluser]
another approach - validate the data and update the database with each form
lots of reasons but start with the user of your forms
you need to validate the data from each form on the server before submitting to database
so if there is a validation problem with form 1
you want to stop the process right there, kick back form 1
and have them fill it out correctly before going to form 2, etc

sessions and cookies are necessary sometimes, but for a multistep form -
you have the perfect reason NOT to bother with them. one hidden form field can
contain a unique id - that use each in form.

for example, in your database table have a varchar field: uniqueid

in your controller BEFORE the first form is called
generate a unique id, this example generates a 10 character id
Code:
$['data']uniqueid =  random_string( 'alnum', 10 );

and then in your form have a hidden field with the unique id value
Code:
echo form_hidden('uniqueid', $uniqueid  );

validate the data from form 1
insert the uniqueid and form 1 to database
in the controller pass uniqueid value to -> $['data']uniqueid
so then its available for form 2 etc etc

then use that uniqueid to do the updates with each form
in other words, you wont need the record or row 'id' which you would typically use for an update

no cookies, no sessions, no problems :-)





#16

[eluser]Lewis Cowles[/eluser]
well in order to have the code handled by multiple controllers, you can simply post the data to the url for the controller and output a field in the view.
Code:
$data['savedFieldData'] = "";
foreach($_POST as $field=>$value){
    $data['savedFieldData'] .= "&lt;input type='hidden' name='{$field}' id='{$field}'";
    $data['savedFieldData'] .= " value='{$value}' /&gt;";
of course a cleaner way of doing this is to have a model generate the output and use the $this->input->post('fieldname'), but for prototyping rapidly, there is no reason not to loop through raw posts (as long as you make a note to go back to the code and make it production ready)

By doing this in a model you would also have the code in the controller cut down dramatically keeping your controllers and views skinny.
#17

[eluser]PhilTem[/eluser]
To avoid manipulation of data you could just add an encoded string that matches the previous form's post data to your new form.

That means:

Code:
// In controller of second form display method
$previous_form = $this->input->post();
$token = $this->encrypt->encode(json_encode($previous_form));

$data = array('hidden_input' => json_encode($previous_form), 'token' => $token));

And for validation you can easily run it as
Code:
$previous_previous_form = $this->input->post('hidden_input');
$token = $this->input->post('token');

if ( $previous_previous_form == $this->encrypt->decode($token) )
{
  // is valid
}
else
{
  // is not valid i.e. manipulated
}

This process can be done over and over again for each form page except you have to generate new tokens and take the right post fields into your hidden input.


NOTE
However, I do not recommend to use this (see posts above). I'd really store all data in a flat file or db. It is just shown by me since you were asking which ways there are and I am solely elaborating one of the possible ways Wink

PS: I really tried auto-completing some code parts using ctrl+space... But unfortunately these forums don't have auto-completion Sad
Big Grin
#18

[eluser]henry178[/eluser]
In controller 1:


Code:
data for the view...


if ($this->form_validation->run() == FALSE)
    {
    
    $data['content'] = $this->load->view('form_part_1', $data, TRUE);  
  
    $this->load->view('form_registration1', $data);
    
    }
    else // FORM 2 * ** *
    {

    
    "model for save my data with last insert id"
    
    $data['content'] = $this->load->view('form_part_2', $data, TRUE);
    
    $this->load->view('form_registration1', $data);
    
    }


In controller 2: //last controller called from the action form
Code:
data for the view...

if ($this->form_validation->run() == FALSE)
    {
    
    $data['content'] = $this->load->view('form_part_2', $data, TRUE);  
  
    $this->load->view('form_registration1', $data);
    
    }
    else // FORM 2 * ** *
    {

    
    "model for save my data"
    
    $data['content'] = $this->load->view('form_part_3', $data, TRUE); //confirm page
    
    $this->load->view('form_registration1', $data);
    
    }




but so I must repeat in controller 2 the same code of controller 1; is it correct?






#19

[eluser]cartalot[/eluser]
>but so I must repeat in controller 2 the same code of controller 1; is it correct?

its possible to do that way, but typically if you have something like a 3 part form -- you would do it from the same controller
just use different method (function) names like

function validform()
function validformtwo()
function validformthree()

so if your class is 'register'
the first form will direct to "register/validform"
second form goes to "register/validformtwo"

#20

[eluser]mariepizzer[/eluser]
Thank you so much, this worked for me. I opened the form before echoing $content and everything was ok (afaik).

[quote author="henry178" date="1341568762"]In controller 1:


Code:
data for the view...


if ($this->form_validation->run() == FALSE)
    {
    
    $data['content'] = $this->load->view('form_part_1', $data, TRUE);  
  
    $this->load->view('form_registration1', $data);
    
    }
    else // FORM 2 * ** *
    {

    
    "model for save my data with last insert id"
    
    $data['content'] = $this->load->view('form_part_2', $data, TRUE);
    
    $this->load->view('form_registration1', $data);
    
    }

[/quote]




Theme © iAndrew 2016 - Forum software by © MyBB