Delete Data without Refreshing Page
Topics related to client-side programming language.
Post questions and answers about JavaScript, Ajax, or jQuery codes and scripts.
-
JanMolendijk
- Posts:282
- Location:Holland Rotterdam
Delete Data without Refreshing Page
Happy New Year Chief.... My first two weeks I spended on bed due to illness
I wanna use a script but it does not delete the item from my sql record
Code: Select all
<?php
$result = $database->prepare ("SELECT * FROM groups ");
$result ->execute();
for ($count=0; $row_member = $result ->fetch(); $count++){
$id = $row_member['post_id'];
?>
<tr class="delete_mem<?php echo $id ?>">
<td><?php echo $row_member['user_name']; ?></td>
<td><?php echo $row_member['group_title']; ?></td>
<td><?php echo $row_member['group_date']; ?></td>
<td width="80">
<a class="btn btn-danger" id="<?php echo $id; ?>">Delete</a>
</td>
</tr>
<?php } ?>
I think it does not detect the id in this javascript
Code: Select all
<script type="text/javascript">
$(document).ready(function() {
$('.btn-danger').click(function() {
var id = $(this).attr("id");
if (confirm("Are you sure you want to delete this Member?")) {
$.ajax({
type: "POST",
url: "delete_member.php",
data: ({
id: id
}),
cache: false,
success: function(html) {
$(".delete_mem" + id).fadeOut('slow');
}
});
} else {
return false;
}
});
});
</script>
&nd here is the code delete_member.php
Code: Select all
<?php
include ('database.php');
$id = $_GET['id'];
$delete_data"delete from groups where post_id = '$id' ";
$database->exec($delete_data);
?>
I hope you can help me out with this one ????
Admin
Posts:805
Hello and Happy New Year.
1. Because the button is an anchor (<a>), add
preventDefault() in the function from 'click', it prevents the action to not open/refresh the page. The function must contains the "event" parameter; in the code below is "ev":
Code: Select all
$('.btn-danger').click(function(ev){
ev.preventDefault();
var id = $(this).attr("id");
// The rest of your code ...
)};
2. The ajax request sends data via POST, so, in php you should use $_POST.
JanMolendijk
Posts:282
I changed the code but still the record is not getting deleted
Code: Select all
<script type="text/javascript">
$(document).ready(function() {
$('.btn-danger').click(function(ev){
ev.preventDefault();
var id = $(this).attr("id");
if (confirm("Are you sure you want to delete this Member?")) {
$.ajax({
type: "POST",
url: "delete_member.php",
data: ({
id: id
}),
cache: false,
success: function(html) {
$(".delete_mem" + id).fadeOut('slow');
}
});
} else {
return false;
}
});
});
</script>
Code: Select all
<?php
include ('database.php');
$id = $_POST['id'];
$delete_data"delete from groups where post_id = '$id' ";
$database->exec($delete_data);
?>
Could there something else what is missing ????
Admin
Posts:805
There is a syntax error in php.
Add "=" after $delete_data in php:
Code: Select all
$delete_data ="delete from groups where post_id = '$id' ";
JanMolendijk
Posts:282
O God that I did not see this `stupido me`....
Thank you very much for all the support `its working now`
Similar Topics
- GET_id not working in UnLink (delete file)
PHP - MySQL
First post
I searching for an hour for a solution; unlink seems not to work with GET id
<?php
$id = (int) $_GET ;
echo $_GET ;
$file = fopen( '.$_GET...
Last post
Here is an answer `o god after 2 hours shame on me for this one`
<?php
$file_pointer = $_GET .'.txt';
if (!unlink($file_pointer)) {
echo (...