get post trong php code example

Example 1: get all post values in php

//Loop all POST values.
foreach ($_POST as $key => $value) {
    echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>";
}

Example 2: php receive post

// Send data trough e.g. AJAX in JavaScript

$.ajax({
    type: "POST",
    url: 'example.php',
    data: { "num1": 1, "num2": 2},
    contentType: "application/json; charset=utf-8",
    dataType: "JSON",
    async: false
})

// You would receive it like this:
  
$num1 = $_POST["num1"];
$num2 = $_POST["num2"];

$sum = $num1 + $num2;
echo $sum;

// Would output: 3

Tags:

Php Example