0

I'm trying to send data to a PHP file.

I tried adding {headers:{'content-type: application/JSON'}}, but I still can't access the data in PHP.

Javascript:

axios.post("http://localhost/Contact_Manager/src/fetchcontacts.php",{action:'getcontacts'})
  .then(res => {
    let posts = res.data;
    commit('setposts',posts)
  })
  .catch(err => console.log(err))

PHP:

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: *');
header('Access-Control-Allow-Headers: *');
$db = mysqli_connect("localhost","root","","contacts");
if(!$db){
  die("failed to connect to database".mysqli_connect_error());
}
$recieved_data=json_decode(file_get_contents("php://input"),true);
var_dump($recieved_data->action)
?>

There's a warning in the PHP file:

Notice: Trying to get property 'action' of non-object in project directory

Dharman
  • 30,962
  • 25
  • 85
  • 135
Dhanush reddy
  • 79
  • 1
  • 5
  • You need to stop manually checking for errors. Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) and [Should I manually check for errors when calling “mysqli_stmt_prepare”?](https://stackoverflow.com/q/62216426/1839439) – Dharman Nov 28 '20 at 12:20

1 Answers1

0

Just change your last line to var_dump($received_data); to see what is really received and it clearly shows that action is not an attribute (and $received_data is not an object) but an array. Hence, you can't use the -> notation but need to use the [] notation to access the attribute´s value:

array(1) {
["action"]=>
string(4) "test"
}

var_dump($recieved_data["action"]); should produce the right output.

Background: Passing true as the second parameter for json_decode() tells PHP to parse your input as an associative array instead of an object (see PHP Documentation). If you want to use it as an object (i.e. use $received_data->action) you need to remove the second parameter or set it to false.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Johannes
  • 1,478
  • 1
  • 12
  • 28
  • That didn't help either mate, I am still getting null as output in my localhost. Thanks! – Dhanush reddy Nov 28 '20 at 10:11
  • `null` where? In your `axios` callback or inside the PHP file? I tried your code with my changes and added a `console.log(res.data)` inside the `then` part of the axios promise and it is outputting the right data. However, you are not returning any output JavaScript can then deal with as `var_dump()` outputs only some readable representation of the variable. – Johannes Nov 28 '20 at 10:23
  • php file is outputting null and my then part in js is also not behaving appropriately as well. I think the problem is with localhost address I am giving to axios. Thanks ! – Dhanush reddy Nov 28 '20 at 11:08
  • By the way way my console logged output looks like:
    C:\wamp64\www\Contact_Manager\src\fetchcontacts.php:10:string 'getcontacts' (length=11)
    
    – Dhanush reddy Nov 28 '20 at 11:11
  • Thanks mate , you were correct . I wasn't really anticipating what was happening there. Thank you for being such a sport ! – Dhanush reddy Nov 28 '20 at 11:47
  • You are welcome! If this answer solves your problem please mark it as `accepted` – Johannes Nov 28 '20 at 22:46