GET vs. POST Best Practices

后端 未结 10 2125
南方客
南方客 2020-11-27 19:44

For my web application (PHP/MYSQL), I show a list of items and a link on each row to delete the item. Right now, the link is



        
10条回答
  •  情书的邮戳
    2020-11-27 20:32

    You don't want to use Get because of the REST principle of not allowing Gets to change the state of the system. Unless you like search engines to delete all your content.

    You won't need a form for each item; you can use one method=post form around the list with a delete_{id} input type=submit. Or, more cleverly, . Names are allowed on submit buttons.

    Per your question in the comments, might work better, though it suffers from some issues that would work badly for localized sites. You can always revert to a HTML Button for a little more control over the presentation. It acts like a submit button by default.

    The fact that you may get extra, unwanted information in the submission is mitigated by the average-case of sending back a much larger page than necessary with your proposed form-per-item solution, when you're just viewing the list. You can always use javascript to substitute the behavior of the plain-old-html form with a smarter version for javascript-capable clients.

    If you're going to link to a "Get" for deletion, you should return a confirmation page with that Get that actually does a post upon confirmation.

提交回复
热议问题