Welcome Guest, Not a member yet? Register   Sign In
problem with form data
#1

(This post was last modified: 04-11-2023, 07:51 AM by anuragk.)

i am trying to get data on Controller from view but not getting anything, tried searching on google and changing codes but nothing works.

Controller

Quote:public function appointmentForm()
{
helper('form');
$rules = [
'rulescheck' => 'required',
'name' => 'required',
];

if(!$this->validate($rules))
{
return $this->rules();
}
else
{
echo 'validation good';
}
}

no matter what only if get called

View

Quote:<?= validation_list_errors() ?>
  <form action="<?=base_url();?>appointment/" method="post" name="rulesForm">
  <?= csrf_field() ?>
  <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>

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


if i try to get the value using this method $post = $this->request->getPost();
this does not work

if i change the route to $routes->post('appointment', 'Appointment::appointmentForm');
I am getting Class and Method error and view is not loading.


I am not getting data on the controller.
Reply
#2

Which version of CI4 are you using, and can you share a list of all your routes?

needless to ask you have both

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

and

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

in place. Then check what routes are available in the debugbar in the browser.

CodeIgniter Wizard (CRUD code generator for Mac) instantly scaffolds Bootstrap-based web applications with an administrative interface (admin templates include Bootstrap5)

Reply
#3

(This post was last modified: 04-11-2023, 11:29 PM by anuragk.)

(04-11-2023, 12:59 PM)gosocial2 Wrote: Which version of CI4 are you using, and can you share a list of all your routes?

needless to ask you have both

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

and

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

in place. Then check what routes are available in the debugbar in the browser.

the current version is 4.3.3 (The latest installed with composer).


route page
Quote:$routes->get('/', 'Home::index');

//coutom routes
$routes->get('contactus', 'Home::contact');
$routes->get('rules', 'Appointment::rules');
$routes->get('appointment', 'Appointment::appointmentForm');
$routes->get('drkpkushwaha', 'Appointment::index');

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

I get this error

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

security is good but
I don't understand one thing, why they are changing CI in such a manner that it is becoming so hard to use.
Reply
#4

It seems you don't understand HTTP request methods (HTTP verb).
See https://codeigniter4.github.io/CodeIgnit...at-is-http
and https://codeigniter4.github.io/CodeIgnit...http-verbs

If you don't care about security, you can use legacy auto routing.
See https://codeigniter4.github.io/CodeIgnit...ing-legacy
Reply
#5

(04-11-2023, 11:51 PM)kenjis Wrote: It seems you don't understand HTTP request methods (HTTP verb).
See https://codeigniter4.github.io/CodeIgnit...at-is-http
and https://codeigniter4.github.io/CodeIgnit...http-verbs

If you don't care about security, you can use legacy auto routing.
See https://codeigniter4.github.io/CodeIgnit...ing-legacy

Thank you sir for your reply.

can you please tell me what should I do in my case? how it will work?
there is no tutorial on the web for your latest CI version, all the tutorial available are old and does not work.
Reply
#6

What should you do is to define all routes you need.
If you don't define the route, you cannot access to the controller method.

And each HTTP request needs a HTTP method (HTTP verb).
Each route also needs a HTTP method (HTTP verb).

You can see your all routes with `php spark routes` command.
https://codeigniter4.github.io/CodeIgnit...ing-routes

You should try the News tutorial:
https://codeigniter4.github.io/CodeIgnit...index.html
Reply
#7

(04-12-2023, 12:09 AM)kenjis Wrote: What should you do is to define all routes you need.
If you don't define the route, you cannot access to the controller method.

And each HTTP request needs a HTTP method (HTTP verb).
Each route also needs a HTTP method (HTTP verb).

You can see your all routes with `php spark routes` command.
https://codeigniter4.github.io/CodeIgnit...ing-routes

You should try the News tutorial:
https://codeigniter4.github.io/CodeIgnit...index.html


Sir, I tried the News Tutorial already but still i don't get any data on my controller.
Quote:helper('form');
$data = $this->request->getPost();

print_r($data);

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

If you defined the route for `POST /appointment`, the Appointment::appointmentForm() method would run
when you post.

What's your problem?
What does the print_r($data) show?
Reply
#9

(04-12-2023, 12:46 AM)kenjis Wrote: If you defined the route for `POST /appointment`, the Appointment::appointmentForm() method would run
when you post.

What's your problem?
What does the print_r($data) show?

it show blank array
Array ( )
Reply
#10

When you post the HTML form?

Can you show all code for the controller and the view?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB