@lado No. Your backend should be like:
<?php
require 'connection.php';
$name = $_POST['name'];
echo 'Name: ' . $name; // add this line so you can see what you receive as name on your back-end
$sql = "SELECT * from nameTab where name='$name'";
$result = mysqli_query($con, $sql);
while($row = $result->fetch_assoc()) {
echo "username: " . $row["username"] . "with id: " . $row["ID"];
}
?>
Instead of $_POST['name']
you could try:
// (remove it) $name = $_POST['name'];
$request = json_decode(file_get_contents("php://input"));
echo 'JSON: ' . $request;
$name = $request->name;
echo 'Name: ' . $name;
(Because you are sending a JSON)
Your front-end should be like:
check() {
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
let postParams = { name: this.name };
this.http.post("http://site.info/php/check.php", JSON.stringify(postParams), options)
.subscribe(data => {
console.log('data', data); // first see what you receive as data
}, error => {
console.log(error);
});
}
If you don’t receive the name (echo 'Name: ' . $name;
outputs nothing as the name), try without the stringify:
this.http.post("http://site.info/php/check.php", postParams, options)
Or try with FormData
:
let body = new FormData();
body.append('name', this.name);
this.http.post("http://site.info/php/check.php", body , options)
I’m not a http expert though, but you can search examples in the web, like: