Search code examples
phppostput

Receiving POST and calling same php by PUT


I have a php page where I receive by post an id called rfc, however, after a few operations I call the same page again, which was originally invoked by a POST but now int he form I set it to be by PUT, so at start of php I have something like:

if ($_SERVER['REQUEST_METHOD'] === 'PUT') {

    $pedidos = array();

    echo $_PUT['rfc'];

}

else {

    include("connectDB.php");
    $mySQL = new MySQL();
    $rfc=$_POST['rfc'];
    ......

First time I get to the page from the form submission with POST, al happens OK, but when I do the PUT, at same page, error log says:

Undefined index: rfc[...]

I thought the is else block would solve this but it doesn't.

This is my form where I do the PUT:

echo "<form action=\"checkout.php\" method=\"PUT\">";
    foreach ($pedidos as $key => $value) {
        echo "<input type=\"hidden\" name=\"pedidos[]\" value=\"$key\">";
        echo "<input type=\"hidden\" name=\"cantidades[]\" value=\"value\">";
        echo "<input type=\"hidden\" name=\"rfc\" value=\"$rfc\">";
    }
echo "<input type=\"submit\" value=\"Confirmar\">";

Line where it claims to be undefined is actually the one from the implicit POST block.


Solution

  • That's because $_PUT doesn't exist. You need a little more work to get PUT data. You essentially need to use:

    $data = file_get_contents('php://input')
    

    (thanks @Jack)