skip to Main Content

I am new with CodeIgniter..
I have weird issue, i can’t get POST request data inside controller.
I thought it is because some inputs, i removed and left only one (in addition to CodeIgniter’s generated csrf_test_name). It didn’t help, then i thought it is because camelCase style name (saveAgreement) and i replaced it with just save, and this also didn’t help..
Of course i refreshed the page, tried with another browser etc.
I share a part of my code with you..

<form action="https://test.mydomain.ee/agreements/saveAgreement/" method="post" accept-charset="utf-8">
    <input type="hidden" name="csrf_test_name" value="fefe3fb945d5d967bb962b5f1a8931eb">
    <input type="text" name="testin">
    <button>Salvesta</button>
    <a href="https://test.mydomain.ee/agreements">Loobu</a>
</form>

And in controller i just try do die-dump the request content and it returns array()

function saveAgreement() 
{
    dd($this->request->getPost());
}

All other forms in same and in other controllers work just fine with same code:

$this->request->getPost()

Thanks for any help!

2

Answers


  1. Chosen as BEST ANSWER

    This was due to the slash in the end of form action parameter. It worked just fine if i changed it to:

    <form action="https://test.mydomain.ee/agreements/saveAgreement" method="post" accept-charset="utf-8">
    

  2. When using CSRF it’s a good idea to use the Codeigniter Form Class:

        echo form_open('agreements/saveAgreement', ['csrf_id' => 'csrfForm', 'id' => 'data-form', 'accept-charset' => 'utf-8', 'class' => 'pl-3 pr-3']);
    

    also, in your controller to grab the identified elements so you are not getting injected items:

        $fields['testin'] = $this->request->getPost('testin');
    

    Finally, in the controller, to grab all items being posted from the form, I still use:

        echo '<pre>';print_r($_POST);echo '</pre>';die;
    

    Helps to catch form element misspellings.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search