On my website, I am trying to remove (hide) an sql entry in a table without refreshing the screen.
Currently, each visible entry is displayed in a div with an id of schedule000 where 000 is the id number of the entry. Each has a delete button:
<div id="btn_delete" class="schedule-delete" onclick="deleteEvent(schedule'.$row['id'].');"></div>
while the function is
function deleteEvent(id) {
var delurl = "..schedule/index.php?d="+String(id.id.substring(8));
$.get(delurl);
$(id).hide('slow');
return false;
}
I found that get function after searching the internet but I couldn't seem to get it to work. I'm looking for suggestions or a new solution.
Thanks
as a side note: this is part of the page that it is calling
if (isset($_GET['d'])) {
$id = $_GET['d'];
$query = "UPDATE schedule SET visible=0 WHERE id='$id'";
if (!(mysql_query($query) or die(mysql_error()))) {
echo 'failed to delete';
}
EDIT: Using Sven's method, I now have:
function del_item() {
try {
var item_id = $(this).attr('item_id');
var item = $(this).parent().parent(); //parent().paren... until you reach the element that you want to delete
$.ajax({
type: 'POST',
url: "../schedule/index.php",
data: { id: item_id},
success: function() {
//fade away and remove it from the dom
$(element).fadeOut(300, function() { $(this).remove(); });
},
error: function() {
alert('failed to delete');
}
});
} catch (err) {
alert(err.message);
}
}
along with the document ready function and
if (isset($_POST['action'])) {
if ($_POST['action'] == 'd' && isset($_POST['id'])) {
$id = $_POST['id'];
$query = "UPDATE schedule SET visible=0 WHERE id='$id'";
if (!(mysql_query($query) or die(mysql_error()))) {
echo 'failed to delete';
}
die("J! :)");
}
GET
method for actually deleting data is a bad idea, that is whatPOST
is for. Please stop writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the deprecation process. Instead you should learn about prepared statements and use either PDO or MySQLi. If you care to learn, here is a quite good PDO-related tutorial. (Also: send data to../schedule/
, not..schedule/
.) – DCoder Aug 19 '12 at 14:28