Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter form POST Empty in Controller from View

I'm working with Codeigniter. I have created an HTML form which post data to the Controller.

This Form was working perfectly but it suddenly stopped posting data.

HTML:

<form name="frm_search" id="frm_search" method="post" action="http://ip/free/index.php/taskdetails/tabOne/1/ShibNo/asc/">
<input  id="order1" name="order1" value="26" type="text" >
<input  id="item1" name="item1" value="" type="text" >
</form>
  1. I tried to check the data post in Browser and it shows the data that I input in the input box.

enter image description here

  1. In My index.php I placed the following code to see if it is posted:
echo "CONTENT_TYPE: " . $_SERVER['CONTENT_TYPE'] . "";
$data = file_get_contents('php://input');
echo "DATA: <pre>";
var_dump($data);
var_dump($_POST);
echo "</pre>"; 
exit; 

RESULT:

CONTENT_TYPE: application/x-www-form-urlencoded

string 'order1=26&item1='

array (size=23)
'order1' => string '26' (length=2)
'item1' => string '' (length=0)

But when I print $_POST in controller I get the following:

Array
(
    [order1] => 
    [item1] =>
)
  1. My .htaccess code as follows:

    RewriteEngine on
    
    RewriteCond $1 !^(index\\.php|resources|robots\\.txt)
    
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    

Can someone tell me what's the issue? I'm not able to find out why is it when the data passes to controller it becomes null. The application was running successfully for more than a year but I got this issue suddenly.

Thank you in advance. Kindly let me know if you need any more to help me.

Note: I figured out that once the page is loaded, If I edit the name of the input to some other name then the data is post successfully.

i.e: instead of order1, If I change the name my editing using inspect element in Chrome to "neworder" Then if I submit, the data submits with value to controller. Don't know why?

Update: I found some this strange. In the set-cookie of the browser. It's like a full page of junk.

Each time when the page is called i start a new session but i wont kill the existing session. Is that a problem?

Even If i destroy the old session and start new one I get eh set-cookies in browser. It has to me one.. but in my case I get more than 6 set cookies in the browser.

like image 573
DonOfDen Avatar asked Mar 02 '16 19:03

DonOfDen


3 Answers

It could be a .htaccess issue with RewriteRule, according to this CI github post issue https://github.com/bcit-ci/CodeIgniter/issues/242:

if Apache server is used then mod rewrite should be enabled, otherwise redirection will be proper but with empty $_POST.

&

if you don't have a trailing slash on your URL the server will send a 301 redirect which will interfere with POST data.

Your problem could be with

RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Try changing to:

RewriteRule ^(.*)$ index.php/$1 [PT,L]

(from: not able get post form submit values in codeigniter) or try to set:

sudo a2enmod rewrite

Hope it helps!

like image 115
JP. Aulet Avatar answered Nov 17 '22 01:11

JP. Aulet


Hi as par my research about this problem is a very unacceptable type but yet I don't know why this is happening or maybe in Codeigniter have some restriction for this kind of problem.

if you described your base URL in config like this, remember at last of URL not included slash:

http://ip/free/index.php

and on your HTML file on the form tag at the action described like this. remember at last used slash:

http://ip/free/index.php/taskdetails/tabOne/1/ShibNo/asc/

This is occurring problem to not pass any data to the controller because 'slash' change the path.

In this case, if you did not use a slash at last on-base URL then don't use on HTML form also to make it work.

If you face this kind of problem then once try this might help.

like image 32
heySushil Avatar answered Nov 17 '22 01:11

heySushil


I was with the same error and solve like the answer above.

baseURL in the .env file must contain for production something like

https://www.yourwebsite.com.br/ CodeIgniter 4

like image 1
Rafael Avatar answered Nov 16 '22 23:11

Rafael