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: 244
- 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
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
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
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
O God that I did not see this `stupido me`....
Thank you very much for all the support `its working now`
Similar Topics
-
Delete part of query string from url without page reload
JavaScript - jQuery - Ajax
The delPartUrl() function from the following JavaScript code can be used to delete part of query string from page url in browser, without affecting...
-
SSEP - BUT Cannot save its Page Data
Scripts from this website
First post
Every time I try to index the site it comes back with this message for some of the pages:
BUT Cannot save its Page Data
It indexed 323 pages but...
Last post
nice, and nice script, you know what your doing
If you ever want to write a post, article or blog...
Maybe on PHP Resources ?
Id be glad to add it...
-
Link to part of another page (not working)
PHP - MySQL
First post
I`m searching for few hours now but can`t find a solution to link to a part off a page ??? I try to use this...
In one comment-page I have this...
Last post
It is strange I tried alot things nothing working correctly.
Meight my Question is wrong, Because I try from a page a link to another page but the...
-
SSEP - where determine part of the page
Scripts from this website
First post
Just one more question
When it displays a part of the pages Content
Where does the script determine where in the page to pull the content from?
Last post
I finally finished up the PHP Resources page
And added a few links to this website
Hope that was ok
-
SSEP-Multiple Results from a Single Page
Scripts from this website
First post
Hi,
SSEP - Site Search Engine PHP-Ajax is a great script.
I only wish to index a single page and have multiple search results appear for that...
Last post
Hi,
Thanks for clarifying that. Not suitable for my purpose, but still an excellent script.
Regards,