receive post request in php code example
Example 1: php post
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['fname'];
}
?>
Example 2: post request php
$response = httpPost("http://mywebsite.com/update.php",
array("first_name"=>"Bob","last_name"=>"Dillon")
);
function httpPost($url, $data){
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
Example 3: php receive post
$.ajax({
type: "POST",
url: 'example.php',
data: { "num1": 1, "num2": 2},
contentType: "application/json; charset=utf-8",
dataType: "JSON",
async: false
})
$num1 = $_POST["num1"];
$num2 = $_POST["num2"];
$sum = $num1 + $num2;
echo $sum;