0

I am doing GET request like this:

//url = http://localhost/api/login.php/?name=max

if(isset($_GET['name'])){
  echo "Hi ".$_GET['name'];
}else{
  echo "Error";
}

But I am unable to fetch data from POST request.

//url = http://localhost/api/login.php/?name=max

if(isset($_POST['name'])){
  echo "Hi ".$_POST['name'];
}else{
  echo "Error";
}

How can I get post data.

RajB009
  • 367
  • 1
  • 5
  • 17
  • 1
    That's not how you pass post data. name is still in the query string. – Devon Oct 30 '18 at 12:11
  • 2
    Possible duplicate of [How do I send a POST request with PHP?](https://stackoverflow.com/questions/5647461/how-do-i-send-a-post-request-with-php) – Jaquarh Oct 30 '18 at 12:14

2 Answers2

0

$_POST data is read from the body of the request if the content-type is set to application/x-www-form-urlencoded or multipart/form-data. You can also read the request body via the php://stdin stream.

Data is not read from the query string, like $_GET.

Devon
  • 32,773
  • 9
  • 61
  • 91
0

You cant get $_POST data from the url, what you can do is this;

<form action="http://localhost/api/login.php" method="post">
<input type="hidden" name="name" value="max">
<input type="submit">
</form>

Or if you want people to set their name;

<form action="http://localhost/api/login.php" method="post">
<input type="text" name="name" value="max">
<input type="submit">
</form>

It's pretty simple and clear :)