How to do a HTTP PUT/DELETE? (Using jquery) . Im making a REST-ful twitter-type aplication using Java Servlets etc... and i have the methods for handling PUT and DELETE, i just need to be able to perform these e.g. when a user submits a form (with the id of the tweet to be deleted, for example). However, forms only support the methods POST and GET hence i was instructed to use jquery, which i am unfamiliar with. Can anyone help me?

6 Years
Discussion Span
Last Post by jimforsyth

Hi jbennet,

Go download the latest version of JQuery (http://jquery.com/) and include this (the downloaded file) in the '<head>' of your site.

Next, create a file called delete.js file with the following 'post' request - and include it on your page.

function delete_item(id) {	
	/* Post the id of the item you want to delete */
	$.post("action.php", { delete_id: id }, 	
	function(data) {
		/* Receive the data from your php file */
		var db_response = eval('(' + data + ')');
		/* Alert the response */

Then create a file called action.php file that handles the deletion of your items. The output from this file will be picked up by the delete.js file, so you can either show a success or an error message.

/* Delete your item using a MySql statement, then output one of the following: */

/* Success! */
//echo json_encode( array('success') );

/* Error :( */
//echo json_encode( array('error') );

Then put it all together like this:

<!-- This is the JQuery file you downloaded -->
<script type="text/javascript" src="jquery-1.5.min.js"></script>
<!-- This is your file that posts a request to your php file -->
<script type="text/javascript" src="delete.js"></script>
<a href="#" onclick="delete_item(2); return false;">Click here</a> to delete item #2

I hope this helps you out.

Jim :)

Edited by jimforsyth: n/a

Votes + Comments
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.