How to get post data in Codeigniter controller?

Member

by daisha , in category: PHP Frameworks , 9 months ago

How to get post data in Codeigniter controller?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by wilmer.lemke , 3 months 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.