jimforsyth 31 Newbie Poster

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 */
		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 :)

jbennet commented: thanks +25
jimforsyth 31 Newbie Poster

Hi rajeesh_rsn,

Another option would be to do the following:

/* Original array */
$my_arr = array( "first","second","third","forth","fifth","sixth" );

/* Remove element from array */
$my_arr = remove_item_from_array( "third",$my_arr );

/* Display aletered array on screen */
print_r( $my_arr );

/* The function */
function remove_item_from_array($val,$arr) {
	/* Find value's index within the array */
	$index = array_search($val,$arr);
	/* Remove value */
	unset($arr[$index]);
	/* Return altered array with the first index reset to 0 */ 
	return array_values($arr);
}

Whichever solution you choose to use, I hope your project goes well.

Jim :)

karthik_ppts commented: Useful post +6