Search code examples
formscodeignitervalidationcodeigniter-4

problem with form data (View & Controller & Routing) in Codeigniter 4.3.3


i am trying to get data on Controller from view but not getting anything just blank array.

View Page

<form action="<?=base_url();?>appointment/" method="post" name="rulesForm">
  <div class="form-group mt-3">
  <input type="text" name="name" />
  <input style="height: 14px;" type="checkbox" name="rulescheck" id="rulescheck" required/>
  </div>
  <div class="text-center"><button type="submit" name="rulesSubmit" id="rulesSubmit">Submit</button></div>
</form>

Controller Page

public function rules()
{
    helper('form');
    return view('pages/rules')
}


public function appointmentForm()
{
    helper('form');
    $data = $this->request->getPost();
    
    print_r($data);
}

Route Page

$routes->get('appointment', 'Appointment::appointmentForm');

i tried changing the route to

$routes->post('appointment', 'Appointment::appointmentForm');

i am getting this error

404 Can't find a route for 'get: appointment'.

now i am using this route

$routes->match(['get', 'post'], 'rules', [Appointment::class, 'rules']);
$routes->match(['get', 'post'], 'appointment', [Appointment::class, 'appointmentForm']);

but still no data in the array.


Solution

  • The problem was with trailing '/' on the form action. I tried this instead and everything is fine now

    <form action="<?= base_url('appointment'); ?>" method="post" name="rulesForm">