2

I'm trying to update the record through a html form. But I have a problem that when I click SUBMIT then if ($ _SERVER ['REQUEST_METHOD'] == "PUT") {} is not activated.

My form html:

<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
           
            ID : <input type="text" name ="idCar1">
            NAME CAR: <input type="text" name="nameCar1" >
            YEAR CAR: <input type="text" name="yearCar1">
            <input type="hidden" name='_METHOD' value="PUT">
            <input type="submit" value="SUBMIT">
            
            <br><br>
            <span style="color:red">* <?php echo $idUpdateErr;?>                 </span>
 
    </form>

And PHP code:

   $idUpdate="";
   $idUpdateErr="";
   if ($_SERVER['REQUEST_METHOD'] == "PUT"){
    
        echo "debug";
        if(empty($_POST["idCar"])){
            $idUpdateErr="ID Car is required";
        }
        else{
            $idUpdate=test_input($_POST["idCar"]);

            $sql = " UPDATE cars 
                    SET name = '".$nameCar."', year= '".$yearCar."'
                    WHERE id= $idUpdate"  ;

            if (mysqli_query($conn, $sql)) {
                echo "Record updated successfully";
                header("Refresh:0");
            } else {
                echo "Error: " . $sql . "<br>" . mysqli_error($conn);
            }
        }

   }
Please help me !!

1 Answer 1

1

In HTML5, You can't set method PUT in a form.

https://softwareengineering.stackexchange.com/questions/114156/why-are-there-are-no-put-and-delete-methods-on-html-forms

What you can do instead is add a hidden field inside the form

<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

            ID : <input type="text" name ="idCar1">
            NAME CAR: <input type="text" name="nameCar1" >
            YEAR CAR: <input type="text" name="yearCar1">
            <input type="hidden" name='_METHOD' value="PUT">
            <input type="submit" value="SUBMIT">

            <input type="hidden" name="_method" value="PUT">
            <br><br>
            <span style="color:red">* <?php echo $idUpdateErr;?>                 </span>

    </form>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.