What's new

Delete method paano to

loktoy

Eternal Poster
Joined
Oct 20, 2014
Posts
1,168
Solutions
2
Reaction
103
Points
497
Age
30
paano ko kaya to gagawin na magkasama ito dalawa code sa iisang file..

bali ito code ko..

<?php
include '../config/config.php';
$id_comment = $_REQUEST['comment_id'];
$id_comment = $_REQUEST['id'];

$sql_delete = mysqli_query($con, "DELETE FROM post where comment_id = '$id_comment'");
$sql_delete = mysqli_query($con, "DELETE FROM adminpost where id = '$id_comment'");
header('location:dashboard.php');
?>


ito yun sa dashboard.php ko..
admin:
<a href= 'commentdelete.php?id=<?php echo $id_comment ?>' class = "btn-delete">Delete</a>
user:
<a href= 'commentdelete.php?comment_id=<?php echo $id_comment ?>' class = "btn-delete">Delete</a>
 
Para magkasama ang dalawang code na ito sa iisang file, maaari mong gawin ang sumusunod:

1. Buksan ang isang plain text editor o integrated development environment (IDE) tulad ng Notepad++, Sublime Text, o Visual Studio Code.
2. Lumikha ng bagong file at i-save ito sa isang pangalan na nais mo (halimbawa: "comment_delete.php").
3. Isulat ang mga sumusunod na code sa "comment_delete.php":

PHP:
<?php
include '../config/config.php';

if (isset($_GET['comment_id'])) {
   $id_comment = $_GET['comment_id'];
   $sql_delete = mysqli_query($con, "DELETE FROM post WHERE comment_id = '$id_comment'");
} elseif (isset($_GET['id'])) {
   $id_comment = $_GET['id'];
   $sql_delete = mysqli_query($con, "DELETE FROM adminpost WHERE id = '$id_comment'");
}

header('Location: dashboard.php');
exit;
?>

4. I-save ang file na "comment_delete.php".
5. Sa "dashboard.php" file, i-update ang code ng "Delete" link:

Admin: <a href="comment_delete.php?id=<?php echo $id_comment ?>" class="btn-delete">Delete</a>

User: <a href="comment_delete.php?comment_id=<?php echo $id_comment ?>" class="btn-delete">Delete</a>

Pagsamahin ang dalawang code na ito sa iisang file ay hindi inirerekomenda, subalit kung gusto mo talagang gawin ito, ito ang paraan. Siguraduhin lamang na ang file extension ng iyong file ay .php at walang ibang code sa loob ng file na makakaapekto sa iba pang bahagi ng iyong sistema.
 
Para magkasama ang dalawang code na ito sa iisang file, maaari mong gawin ang sumusunod:

1. Buksan ang isang plain text editor o integrated development environment (IDE) tulad ng Notepad++, Sublime Text, o Visual Studio Code.
2. Lumikha ng bagong file at i-save ito sa isang pangalan na nais mo (halimbawa: "comment_delete.php").
3. Isulat ang mga sumusunod na code sa "comment_delete.php":

PHP:
<?php
include '../config/config.php';

if (isset($_GET['comment_id'])) {
   $id_comment = $_GET['comment_id'];
   $sql_delete = mysqli_query($con, "DELETE FROM post WHERE comment_id = '$id_comment'");
} elseif (isset($_GET['id'])) {
   $id_comment = $_GET['id'];
   $sql_delete = mysqli_query($con, "DELETE FROM adminpost WHERE id = '$id_comment'");
}

header('Location: dashboard.php');
exit;
?>

4. I-save ang file na "comment_delete.php".
5. Sa "dashboard.php" file, i-update ang code ng "Delete" link:

Admin: <a href="comment_delete.php?id=<?php echo $id_comment ?>" class="btn-delete">Delete</a>

User: <a href="comment_delete.php?comment_id=<?php echo $id_comment ?>" class="btn-delete">Delete</a>

Pagsamahin ang dalawang code na ito sa iisang file ay hindi inirerekomenda, subalit kung gusto mo talagang gawin ito, ito ang paraan. Siguraduhin lamang na ang file extension ng iyong file ay .php at walang ibang code sa loob ng file na makakaapekto sa iba pang bahagi ng iyong sistema.
pano ba to dagdagan na dapat ganito

url: 'commentdelete.php', & 'admincommentdelete.php';

para sakali kung alin yun gusto ko idelete yun mapupunta sa url



$('.btn-delete').on('click', function(e) {
e.preventDefault();
const href = $(this).attr('href')
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
$.ajax({
url: 'commentdelete.php',
type: 'POST',
data: {comment_id: <?php echo $id_comment; ?>},
success: function(data) {
console.log(data); // Check the response data
if (data = 'success') {
Swal.fire(
'Deleted!',
'Your file has been deleted.',
'success'
).then(function() {
location.reload();
});
} else {
Swal.fire(
'Error!',
'An error occurred while deleting the file.',
'error'
);
}
},
}

);
}
});
}
);
 
pano ba to dagdagan na dapat ganito

url: 'commentdelete.php', & 'admincommentdelete.php';

para sakali kung alin yun gusto ko idelete yun mapupunta sa url



$('.btn-delete').on('click', function(e) {
e.preventDefault();
const href = $(this).attr('href')
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
$.ajax({
url: 'commentdelete.php',
type: 'POST',
data: {comment_id: <?php echo $id_comment; ?>},
success: function(data) {
console.log(data); // Check the response data
if (data = 'success') {
Swal.fire(
'Deleted!',
'Your file has been deleted.',
'success'
).then(function() {
location.reload();
});
} else {
Swal.fire(
'Error!',
'An error occurred while deleting the file.',
'error'
);
}
},
}

);
}
});
}
);
Para maayos ang code at maipapasa ang tamang URL depende sa uri na nais mong idelete, maaari mong gawin ang sumusunod:

1. Sa "dashboard.php" file, i-update ang code ng "Delete" link:

Admin:
Code:
<a href="commentdelete.php?comment_id=<?php echo $id_comment ?>" class="btn-delete">Delete</a>

User:
Code:
<a href="admincommentdelete.php?id=<?php echo $id_comment ?>" class="btn-delete">Delete</a>

2. Sa JavaScript code, i-update ang AJAX request para maipasa ang tamang URL depende sa uri na idelete. Ang code na ito ay nagbabasa ng href attribute ng <a> tag na kinuha ng user.

JavaScript:
$('.btn-delete').on('click', function(e) {
    e.preventDefault();
    const href = $(this).attr('href');
    Swal.fire({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!'
    }).then((result) => {
        if (result.isConfirmed) {
            $.ajax({
                url: href, // I-update ang URL na ipinapasa sa AJAX request
                type: 'POST',
                data: {comment_id: <?php echo $id_comment; ?>},
                success: function(data) {
                    console.log(data); // Check the response data
                    if (data === 'success') { // I-update ang comparison operator mula sa "=" papunta sa "==="
                        Swal.fire(
                            'Deleted!',
                            'Your file has been deleted.',
                            'success'
                        ).then(function() {
                            location.reload();
                        });
                    } else {
                        Swal.fire(
                            'Error!',
                            'An error occurred while deleting the file.',
                            'error'
                        );
                    }
                },
            });
        }
    });
});

Assuming na ang commentdelete.php ay ang file na ginagamit ng user at ang admincommentdelete.php ay ang file na ginagamit ng admin. Kung iba ang pangalan ng mga file, siguraduhin na i-update ang url sa $.ajax request upang tumugma sa tamang pangalan ng file na gusto mong gamitin.
 

Similar threads

Back
Top