0

I want to send PUT request inside same project to different page from form using PHP. How can i check request? is it POST or PUT?

my code

<form method="POST" action="request.php"> 
    <input type="text" name="name" value="blahblah">
    <button type="submit">UPDATE</button>
</form>

    if($_SERVER['REQUEST_METHOD'] == 'POST')
    {
        //
    }

Thanks, regards

natxchill
  • 15
  • 6
  • https://stackoverflow.com/questions/8054165/using-put-method-in-html-form – Danyal Sandeelo Jan 02 '20 at 09:24
  • `According to the HTML standard, you can not. The only valid values for the method attribute are get and post, corresponding to the GET and POST HTTP methods.
    is invalid HTML and will be treated like
    , i.e. send a GET request.`
    – Danyal Sandeelo Jan 02 '20 at 09:26
  • as I understand it, i will add into form input element and it's attribute name will be "_method". Also i will manual check like this if($_POST['_method'] == 'PUT'). – natxchill Jan 02 '20 at 09:32
  • Does this answer your question? [Using PUT method in HTML form](https://stackoverflow.com/questions/8054165/using-put-method-in-html-form) – Altherius Jan 02 '20 at 11:43

1 Answers1

1

In HTML, as mentionned, forms only accept sending via GET or POST. To send data with the method PUT, You can do it in AJAX (https://api.jquery.com/jquery.ajax/ with JQuery). Unfortunately, you will most probably have to do some work with the page that is receiving the form as you will probably want to return XML or JSON instead of HTML.

Here below a very basic example just to show is at work. You should see the request method (PUT) in your Javascript console.

<?php

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

else {

?>


<script
    src="https://code.jquery.com/jquery-3.4.1.min.js"
    integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
    crossorigin="anonymous"></script>
<script>
    $(function() {
        $.ajax(
            {
                url: '/test.php',
                method: 'put',
                success: function(data) {
                    console.log(data);
                }
            }
        );
    })
</script>

<?php } ?>
Altherius
  • 549
  • 6
  • 21