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
<a href='item.php?id=3&action=delete'>Delete Item</a>
If I wanted to use POST instead... how would I do it (this is a dynamically generated list)? Can I send POST data without the use of a form?
Or, for every item, would I have to do:
<form action='item.php?id={$item_id}' method='POST'>
<input type='hidden' name='action' value='delete'>
<input type='submit' value='delete item'>
</form>
and style the submit button to look like the original link?
I am not familiar with php CURL or REST, would they help address this issue?
Please use POST for anything that modifies persistent state in the database. You don't want crawlers visiting your delete links!
Have a read at Architecture of the World Wide Web, Volume One and URIs, Addressability, and the use of HTTP GET and POST by W3C.
Edit: Sometimes though you need to use GET. For example membership activation URLs which are sent in emails are GET and need to somehow modify the database.
In general it's not a good idea to have a GET request that modifies the system state somehow, like deleting an item.
You could have your form look like this:
<form action='item.php' method='POST' id='form'>
<input type='hidden' name='action' value='delete' />
<input type='hidden' name='id' value='{item_id}' />
<a href="" onclick="document.getElementById('form').submit(); return false;">Delete item</a>
</form>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With