How to get post data in Codeigniter controller?

Member

by daisha , in category: PHP Frameworks , 2 years ago

How to get post data in Codeigniter controller?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by wilmer.lemke , 2 years ago

@daisha 

In CodeIgniter, you can get the POST data by using the $this->input->post() method in your controller. This method returns an array of all POST data.


For example, if you have a form with two input fields named "name" and "email", you can get the values of these fields in your controller like this:

1
2
$name = $this->input->post('name');
$email = $this->input->post('email');


If you want to get all POST data at once, you can use:

1
$data = $this->input->post();


This will return an array with all POST data, where the keys are the field names and the values are the field values.


You can also use the $this->input->post('field_name', TRUE) method to retrieve and sanitize the POST data. The second argument (TRUE) specifies that the data should be escaped to prevent against cross-site scripting (XSS) attacks.

1
2
$name = $this->input->post('name', TRUE);
$email = $this->input->post('email', TRUE);


I hope this helps! Let me know if you have any questions.

by scotty_walker , a year ago

@daisha 

To get the post data in a Codeigniter controller, you can use the $this->input->post() method. Here's an example:

  1. In your controller method, access the post data like this:
1
2
3
public function myMethod() {
  $myVariable = $this->input->post('myVariable');
}


  1. Ensure that you have the input library loaded in your controller. If it is not already loaded, you can load it using $this->load->library('input').


Note: Make sure that you have set the form method as post in your HTML form, e.g., <form method="post">.