php return post data code example
Example 1: get all post values in php
foreach ($_POST as $key => $value) {
echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
}
Example 2: 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 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;