I'm doing POST
from angular to a file in PHP
but the data that is sent is not being recognized in the file PHP
, I have already tried several ways but I still can not read the parameter "op"
in file PHP
.
The request is sent to the PHP file but the data that is sent by POST does not arrive. If I delete the condition of the if I return the data of the querry. But I need to do the verification with the IF and the variable 'op'.
Angular controller:
public getListStudents() {
let headers = new Headers();
headers.append('contentType', 'application/x-www-form-urlencoded');
return this.http.post("http://localhost/obtener_alunos.php", {op: 1},
headers);
}
PHP
<?php
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods", "POST, GET");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-
Type, Accept");
require("conectar_mysql.php");
if ((isset($_REQUEST['op'])) && ($_REQUEST['op'] == 1)) {
$vec_res = array();
$result = mysqli_query($conn, "SELECT * FROM alunos");
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
array_push($vec_res, array('nome' => $row['nome'], 'idade' =>
$row['idade'], 'morada' => $row['morada']));
}
echo json_encode($vec_res);
}
}
mysqli_close($conn);
?>
The problem has already been solved using your suggestions $ request = json_decode (file_get_contents ("php: // input")) - > op; I had to put op to recover the value. What I still do not understand is that the $ _REQUEST or $ _POST does not work.
<?php
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-
Type, Accept");
$request = json_decode(file_get_contents("php://input")) -> op;
require("conectar_mysql.php");
if (isset($request) && ($request == 0)) {
$vec_res = array();
$result = mysqli_query($conn, "SELECT * FROM alunos");
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
array_push($vec_res, array('nome' => $row['nome'], 'idade' =>
$row['idade'], 'morada' => $row['morada']));
}
echo json_encode($vec_res);
}
}
mysqli_close($conn);
?>
Thank you all for your time.