954,566 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Jquery HTTP PUT/DELETE

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?

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
 

Hi jbennet,

Go download the latest version of JQuery ( http://jquery.com/ ) and include this (the downloaded file) in the '' 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 */
		alert(db_response[0]);
	});
}


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:

<html>
<head>
<!-- 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>
</head>
<body>
<a href="#" onclick="delete_item(2); return false;">Click here</a> to delete item #2
</body>
</html>


I hope this helps you out.

Jim :)

jimforsyth
Newbie Poster
8 posts since Apr 2008
Reputation Points: 41
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: