Codeigniter $This->Input->Post() Empty While $_Post Is Working Correctly

$_POST and $this-input-post() are empty

looks like incorrect form action

Try to change :-

<form action="<?php echo base_url(); ?>cms/activiteit/saveAct" method="post">

to

<?php echo form_open('activiteit/saveAct');?>

will auto get your site base url and post to controller(activiteit) method(saveAct)

For more :- https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

leave empty Base url in config or set it to your CI directory http://localhost/yourdir/

input-post and $_POST are empty in CodeIgniter API calling from Angular 4, what is the right way to make a post request in angular 4

EDIT (Perfect Solution):

Found out that the formdata object approach was deprecated so I just included a header in options and included in the API call http.post method which works fine and is much better solution.

constructor(public http:Http) { 
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let options = new RequestOptions({ headers: headers });}

createUser(userName)
{
let body = { username:userName};
return this.http.post("http://localhost/ci-abc/api/create_user",body,this.options)
.map(res => res.json());
}

Deprecated approach (Works but deprecated/not usual practice):

Took few hours but found the solution, I created body as a new formdata object, appended parameters to it as key and their values and it worked fine now I am retrieving through $this->input->post.

  let body = new FormData;
body.append('userid', Userid);
body.append('taskname', TaskName);
console.log(body);
return this.http.post("http://localhost/ci-abc/api/add_task",body)
.map(res => res.json());

Using these headers in the constructor of my codeigniters API controller

header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Access-Control-Allow-Origin');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE');

API method:

 function add_task_post()
{
$userid = $this->input->post('userid');
$taskname = $this->input->post('taskname');

if (!$taskname || !$userid) {
$this->response("Enter taskname and userid to add", 400);
} else
$result = $this->todo_model->add_task($taskname, $userid);
if ($result === 0) {
$this->response("Task could not be added. Try again.", 404);
} else {
$this->response("success", 200);
}
}

$this-input-post() and $_POST is NULL in codeigniter

you need to add the url to your form_open method. Try

echo form_open("your_url");

give the path of your save method instead of your_url.
Also check documentation here

The form_open method demands a target url.

Codeigniter - input post method is removing or emptying the post data from multi select field

try this 'field' => 'subjectID[]' instead of 'field' => 'subjectID' in the validation rules

Codeigniter $this-input-post is always FALSE

you should try

isset($_REQUEST)
or
!empty($_REQUEST)

to check data is coming or not



Related Topics



Leave a reply



Submit