Welcome Guest, Not a member yet? Register   Sign In
How to receive data from Form_A and send to Form_B ? - 26.07.19
#1
Lightbulb 
(This post was last modified: 07-26-2019, 04:13 AM by Porto.)

Hi Guys greetings!

Probably the situation is very simple to solve, but I haven't found the light yet.

1 ° I have a controller and two methods - Method A and Method B.

2° A Method - FORM_A IS SUBMITTED TO FORM_B

3° A Method - Form A collect Form Data through input class: $formData['formfield'] = $this->input->post(NULL, TRUE);

4° A Method - The Form data pass through the validation process.

5° A Method - If the validation process runs it's ok, function "redirect()" redirect to the B Method and must take the Form data with.

____________

1° B Method - FORM_B MUST RECEIVE THE SUBMITTED DATA FROM FORM_A

2° B Method - Form_B is a "second form" where the passed data and the new data must pass through the validation process again. but for that to happen, i need to receive the data from the first form (A Method = Form_A).

3° I tried to collect data through the arrays $formData or $formfield on the Form_B, but i recieve the following error messages.

Severity: Notice

Message:  Undefined variable: formData
Filename: testefolder/form_b.php
Line Number: 13
or
Severity: Notice

Message: Undefined index: username
Filename: testefolder/form_b.php
Line Number: 17

What i did wrong or what is missing please?

Thank you so much in advance!
Below is my code:

******* 1° The Controller and A Method *******


Code:
class Voting extends CI_Controller {

       public function form_a()
       {
       $formData['formfield'] = $this->input->post(NULL, TRUE);

       $this->form_validation->set_rules('username', 'Username', 'required');
       $this->form_validation->set_rules('password', 'Password', 'required');
       $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
       $this->form_validation->set_rules('email', 'Email', 'required');        

               if ($this->form_validation->run() == FALSE)
               {
                      $this->load->view('testefolder/form_a'); // 1° VIEW
               }
               else
               {
                     $this->load->view(redirect('voting/form_b',$formData)); // 2° VIEW
               }
       }
}

******* 2° B Method *******

Code:
    public function form_b()
    {

        $formData['formfield'] = $this->input->post(NULL, TRUE);

        $this->form_validation->set_rules('field1', 'Field1', 'required');
        $this->form_validation->set_rules('field2', 'Field2', 'required');
        $this->form_validation->set_rules('field3', 'Field3', 'required');
        $this->form_validation->set_rules('field4', 'field4', 'required');        

        $this->load->view('testefolder/form_b',$formData); // 2° VIEW

    }
Reply
#2

Hello,
-you could try throught GET requests, so you will have to redirect to form_b with some data on url ?data=1&data=2 etc
-you could load form_b in form_a and include the data in the function like $this->form_b($data1,$data2)
-you could save the valid data to session or maybe throught session flashdata
-if its load view you can add the data through $data['info'] and in the view you will have the $info
-you could save them to a json for a while Tongue
Reply
#3

If you use CI4 in your project then it has such methods as:
https://codeigniter4.github.io/userguide...direct#old
1) old(); - Provides a simple way to access “old input data” from submitting a form.
2) return redirect()->back()->withInput(); - Keep the old input values upon redirect so they can be used by the `old()` function.
I would change this world, but God doesn't give me the source.
Reply
#4

This is an error everyone new to CI does. When you pass your $formData array to the view, the array itself is not available in the view. Each keys are converted to an individual variable. So in your case, to access the value of $formData['formfield'] in your view, you need to use the new variable $formfield that CI has created for you.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#5

(This post was last modified: 08-01-2019, 10:13 AM by Wouter60.)

After succesful form validation in form_a, store the post data into a session variable. Then redirect to voting/form_b.
In form_b, read the session variable into a "normal" array, and pass it to the view "form b".

Controller:
Code:
public function form_a()
{
   if ($this->form_validation->run() == FALSE) }
      // open the view for form a.
   }
   else {
      $this->session->form_a_data = $this->input->post();
      redirect('voting/form_b');
   }
}

public function form_b()
{
   $form_a_data = $this->session->form_a_data;
   if ($this->form_validation->run() == FALSE) }
      // open the view for form b:
      $this->load->view('testefolder/form_b', $form_a_data);
   }
   else {
      //process the output from form b.
       //after that, remove the form_a_data session variable from memory
      unset($_SESSION['form_a_data']);
   }
}

Keep in mind that the view form_b does not know the $form_a_data array itself, because CI extracts it. In form_b you can use the variables $username and $password.
Reply
#6

@Porto,

Have you considered using a form wizard where there is just one form instead of two? Something like this https://colorlib.com/polygon/gentelella/...zards.html .
Reply




Theme © iAndrew 2016 - Forum software by © MyBB