CodeIgniter Forums

Full Version: input post empty
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys,
i'm writing a backend in CI (frontend separated in html/js)
when i tried to send POST data to my controller $_POST is empty..
I've tried for few way but i always receive a empty array.
Maybe the problem could be about htaccess???

tried to call my API 
Code:
http://mysite.com/users/register_post_user
two vars
id=test
email=[email protected]
tried already to set url with or without  / at the end    

my controller - Users.php -
PHP Code:
public function register_post_user()
 
   {
        
print_r($_POST);

                
//few alternative i tried
        //echo $_POST['id']." ".$_POST['email'];  
        //$id = $this->input->post('id');
        //$email = $this->input->post('email');
        //echo $id." ".$email;
    

in that case the result is
Code:
Array ( )

i'm sending request by Postman and my frontend. the result doesn't change.
here my - .htacces - (modified only for removing index.php)

Code:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
which js frawework you are using for posting? As I remember I had similar using when I used Angularjs.
Try 


PHP Code:
$_POST json_decode(file_get_contents("php://input"), true); 

Or if it's not json you're sending without the json_decode
(10-19-2017, 11:21 PM)neuron Wrote: [ -> ]which js frawework you are using for posting? As I remember I had similar using when I used Angularjs.

I tried by Jquery, pure Javascript and tool called Postman.
(10-19-2017, 11:53 PM)ponzo Wrote: [ -> ]Try 


PHP Code:
$_POST json_decode(file_get_contents("php://input"), true); 

Or if it's not json you're sending without the json_decode

I'm sending two easy vars like => page.php?VAR1=value&VAR2=value2
I'm not sending json data
You're showing how you check if the vars were received, but not how you actually make the request.
We need to see your JavaScript code on how you are sending the data etc;
my js function
Code:
function sendLogin(id, email) {
   $.ajax({
       type: "POST",
       contentType: "application/json; charset=utf-8",
       url: "http://www.plickapp.com/API/index.php/users/register_user/",
       data: "{'idfb':'" + id+ "', 'emailfb':'" + email+ "' }",
       success: function (result) {
            console.log("Ok " + result);
       },
       error: function (result) {
           console.log(result);
       }
  });
}

the frontend is hosted in another space (app, localhost, another server)
so i set the header in my controllers for accepting external request with

PHP Code:
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: ACCEPT, CONTENT-TYPE, X-CSRF-TOKEN");
header("Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"); 

could be the .htaccess ?

-------- UPDATE ----------- 
sending data by GET it works.
example
my request

Code:
http://mysite.com/users/register_post_user/?id=5890459084523908&email=testemail

the result is 
Code:
Array ( [id] => 5890459084523908 [email] => testemail )

tested by URL in browser directly and by Postman....
Huh Huh Huh Huh
js:
Code:
function sendLogin(id, email) {
   $.ajax({
       type: 'post',
       url: "http://www.plickapp.com/API/index.php/users/register_user/",
       data: {
           'idfb': id,
           'emailfb': email
       },
       success: function (result) {
            console.log("Ok " + result);
       },
       error: function (result) {
           console.log(result);
       }
  });
}

PHP:
Code:
public function register_post_user()
{
    ob_start();

    print_r($this->input->post());

    //few alternative i tried
    //echo $_POST['id']." ".$_POST['email'];  
    //$id = $this->input->post('id');
    //$email = $this->input->post('email');
    //echo $id." ".$email;

    $output = ob_get_clean();
    $this->output->set_output($output);
}
(10-20-2017, 09:24 AM)ivantcholakov Wrote: [ -> ]js:
Code:
function sendLogin(id, email) {
  $.ajax({
      type: 'post',
      url: "http://www.plickapp.com/API/index.php/users/register_user/",
      data: {
          'idfb': id,
          'emailfb': email
      },
      success: function (result) {
           console.log("Ok " + result);
      },
      error: function (result) {
          console.log(result);
      }
 });
}

PHP:
Code:
public function register_post_user()
{
   ob_start();

   print_r($this->input->post());

   //few alternative i tried
   //echo $_POST['id']." ".$_POST['email'];  
   //$id = $this->input->post('id');
   //$email = $this->input->post('email');
   //echo $id." ".$email;

   $output = ob_get_clean();
   $this->output->set_output($output);
}

It works!!! thank you! Big Grin