What's new

PHP Help paano po gumamit ng SweetAlert 2 sa pag update ng database gamit ang PDO

tadaki27

Honorary Poster
Established
Joined
Jan 24, 2017
Posts
592
Reaction
78
Points
185
Paano ko po ba magagamit ang SweetAlert 2 sa pag update ng database records namin para ma-archive na lang instead of deletion ng record sa database at gamit po namin ay PDO.
kung maari po kahit simple code lang po na may database. please.
 
Sure, here's a simple example on how to use SweetAlert 2 with PDO to update a database record and archive it instead of deleting it.

Assuming you have a table called records with columns id, name, and archived, and you have included the SweetAlert 2 library in your project, here's the PHP code:

PHP:
<?php
// Include PDO connection
require_once 'path/to/your/connection/file.php';

// Check if the form is submitted
if(isset($_POST['submit'])) {
    // Get the record ID from the form
    $recordId = $_POST['recordId'];

    // Update the record and set the 'archived' column to 1
    $sql = "UPDATE records SET archived = 1 WHERE id = :id";
    $stmt = $pdo->prepare($sql);
    $stmt->execute(['id' => $recordId]);

    // Display a success message using SweetAlert 2
    echo "
        <script>
        Swal.fire({
            title: 'Success!',
            text: 'Record archived successfully.',
            icon: 'success',
            showCancelButton: false,
            confirmButtonColor: '#3085d6',
            confirmButtonText: 'OK'
        }).then((result) => {
            if (result.value) {
                // Redirect to another page or perform any desired action
                window.location = 'index.php';
            }
        });
        </script>
    ";
}
?>

<!-- HTML form -->
<form method="POST">
    <input type="hidden" name="recordId" value="<?php echo $recordId; ?>">
    <!-- Other form inputs -->
    <button type="submit" name="submit">Archive Record</button>
</form>

In this code, we first include the PDO connection file and check if the form is submitted. If it is, we retrieve the record ID from the form and update the archived column of the corresponding record to 1.

After the update is successful, we use SweetAlert 2 to display a success message. You can customize the message and appearance of the alert according to your preference.

Once the user clicks the "OK" button on the alert, you can redirect them to another page or perform any other desired action.

Note: Make sure to replace 'path/to/your/connection/file.php' with the actual path to your PDO connection file. Also, adjust the table and column names according to your database structure.
 
Sure, here's a simple example on how to use SweetAlert 2 with PDO to update a database record and archive it instead of deleting it.

Assuming you have a table called records with columns id, name, and archived, and you have included the SweetAlert 2 library in your project, here's the PHP code:

PHP:
<?php
// Include PDO connection
require_once 'path/to/your/connection/file.php';

// Check if the form is submitted
if(isset($_POST['submit'])) {
    // Get the record ID from the form
    $recordId = $_POST['recordId'];

    // Update the record and set the 'archived' column to 1
    $sql = "UPDATE records SET archived = 1 WHERE id = :id";
    $stmt = $pdo->prepare($sql);
    $stmt->execute(['id' => $recordId]);

    // Display a success message using SweetAlert 2
    echo "
        <script>
        Swal.fire({
            title: 'Success!',
            text: 'Record archived successfully.',
            icon: 'success',
            showCancelButton: false,
            confirmButtonColor: '#3085d6',
            confirmButtonText: 'OK'
        }).then((result) => {
            if (result.value) {
                // Redirect to another page or perform any desired action
                window.location = 'index.php';
            }
        });
        </script>
    ";
}
?>

<!-- HTML form -->
<form method="POST">
    <input type="hidden" name="recordId" value="<?php echo $recordId; ?>">
    <!-- Other form inputs -->
    <button type="submit" name="submit">Archive Record</button>
</form>

In this code, we first include the PDO connection file and check if the form is submitted. If it is, we retrieve the record ID from the form and update the archived column of the corresponding record to 1.

After the update is successful, we use SweetAlert 2 to display a success message. You can customize the message and appearance of the alert according to your preference.

Once the user clicks the "OK" button on the alert, you can redirect them to another page or perform any other desired action.

Note: Make sure to replace 'path/to/your/connection/file.php' with the actual path to your PDO connection file. Also, adjust the table and column names according to your database structure.
salamat susubukan ko ito
 
salamat susubukan ko ito
Walang anuman! Kung mayroon kang anumang mga katanungan sa proseso o kahit na anong mga isyung lumitaw habang sinusubukan mo ito, huwag mag-atubiling magtanong. Ako ay narito upang matulungan ka. Magtagumpay sa iyong pagsubok sa paggamit ng SweetAlert 2 at PDO para sa pag-archive ng mga talaan sa iyong database!
 
Walang anuman! Kung mayroon kang anumang mga katanungan sa proseso o kahit na anong mga isyung lumitaw habang sinusubukan mo ito, huwag mag-atubiling magtanong. Ako ay narito upang matulungan ka. Magtagumpay sa iyong pagsubok sa paggamit ng SweetAlert 2 at PDO para sa pag-archive ng mga talaan sa iyong database!
tanong ko lang paano naman kung ang gamitin naman sa sweetalert ay yun may confirm at Cancel? kailangan ba ng Ajax dun?
 
tanong ko lang paano naman kung ang gamitin naman sa sweetalert ay yun may confirm at Cancel? kailangan ba ng Ajax dun?
Kung nais mo gamitin ang SweetAlert 2 na may confirm at cancel buttons, maaari mong isagawa ito nang hindi na kailangang gumamit ng Ajax. Dito ay isang halimbawa ng kung paano ito maaaring gawin:

PHP:
<?php
// Include PDO connection
require_once 'path/to/your/connection/file.php';

// Check if the form is submitted
if(isset($_POST['submit'])) {
    // Get the record ID from the form
    $recordId = $_POST['recordId'];

    // Display a confirmation message using SweetAlert 2
    echo "
        <script>
        Swal.fire({
            title: 'Archive Record',
            text: 'Are you sure you want to archive this record?',
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, archive it!',
            cancelButtonText: 'Cancel'
        }).then((result) => {
            if (result.isConfirmed) {
                // If confirmed, update the record and set the 'archived' column to 1
                let formData = new FormData();
                formData.append('recordId', $recordId);

                fetch('archive.php', {
                    method: 'POST',
                    body: formData
                }).then((response) => {
                    if (response.ok) {
                        return response.text();
                    } else {
                        throw new Error('Error: ' + response.status);
                    }
                }).then((data) => {
                    // Display a success message
                    Swal.fire({
                        title: 'Success!',
                        text: 'Record archived successfully.',
                        icon: 'success',
                        showCancelButton: false,
                        confirmButtonColor: '#3085d6',
                        confirmButtonText: 'OK'
                    }).then((result) => {
                        if (result.value) {
                            // Redirect to another page or perform any desired action
                            window.location = 'index.php';
                        }
                    });
                }).catch((error) => {
                    // Display an error message
                    Swal.fire({
                        title: 'Error!',
                        text: error.message,
                        icon: 'error',
                        showCancelButton: false,
                        confirmButtonColor: '#3085d6',
                        confirmButtonText: 'OK'
                    });
                });
            }
        });
        </script>
    ";
}
?>

<!-- HTML form -->
<form method="POST">
    <input type="hidden" name="recordId" value="<?php echo $recordId; ?>">
    <!-- Other form inputs -->
    <button type="submit" name="submit">Archive Record</button>
</form>

Sa halimbawang ito, kapag i-submit ang form, papakita ng SweetAlert 2 ang isang confirmation message na may confirm at cancel buttons. Kapag pinindot ang "Yes, archive it!" button, gagawin ang Ajax request sa archive.php upang ma-update ang record at itakda ang 'archived' na column sa 1. Kapag matagumpay na na-update ang record, ipapakita ng SweetAlert 2 ang isang success message. Kung ibang status ang ibalik ng Ajax request o mayroong error, ipapakita ng SweetAlert 2 ang isang error message.

Sana makatulong ito! Huwag mag-atubiling magtanong kung mayroon kang ibang mga katanungan.
 
Sure, here's a simple example on how to use SweetAlert 2 with PDO to update a database record and archive it instead of deleting it.

Assuming you have a table called records with columns id, name, and archived, and you have included the SweetAlert 2 library in your project, here's the PHP code:

PHP:
<?php
// Include PDO connection
require_once 'path/to/your/connection/file.php';

// Check if the form is submitted
if(isset($_POST['submit'])) {
    // Get the record ID from the form
    $recordId = $_POST['recordId'];

    // Update the record and set the 'archived' column to 1
    $sql = "UPDATE records SET archived = 1 WHERE id = :id";
    $stmt = $pdo->prepare($sql);
    $stmt->execute(['id' => $recordId]);

    // Display a success message using SweetAlert 2
    echo "
        <script>
        Swal.fire({
            title: 'Success!',
            text: 'Record archived successfully.',
            icon: 'success',
            showCancelButton: false,
            confirmButtonColor: '#3085d6',
            confirmButtonText: 'OK'
        }).then((result) => {
            if (result.value) {
                // Redirect to another page or perform any desired action
                window.location = 'index.php';
            }
        });
        </script>
    ";
}
?>

<!-- HTML form -->
<form method="POST">
    <input type="hidden" name="recordId" value="<?php echo $recordId; ?>">
    <!-- Other form inputs -->
    <button type="submit" name="submit">Archive Record</button>
</form>

In this code, we first include the PDO connection file and check if the form is submitted. If it is, we retrieve the record ID from the form and update the archived column of the corresponding record to 1.

After the update is successful, we use SweetAlert 2 to display a success message. You can customize the message and appearance of the alert according to your preference.

Once the user clicks the "OK" button on the alert, you can redirect them to another page or perform any other desired action.

Note: Make sure to replace 'path/to/your/connection/file.php' with the actual path to your PDO connection file. Also, adjust the table and column names according to your database structure.
sinubukan ko include ito ngunit hindi lumalabas ang sweet alert.
 
sinubukan ko include ito ngunit hindi lumalabas ang sweet alert.
Kapag hindi lumalabas ang SweetAlert, narito ang ilang mga posibleng dahilan at mga solusyon:

1. Siguraduhin na naka-include ang SweetAlert library: Tiyakin na naka-include mo ang tamang library ng SweetAlert sa iyong proyekto. Siguraduhin na mayroon kang link o script tag na naglalaman ng kinakailangang SweetAlert library na ito:

HTML:
<script src="path/to/sweetalert.min.js"></script>
<link rel="stylesheet" href="path/to/sweetalert.css">

2. Tiyaking tama ang iyong mga path: Siguraduhin na tamang ang iyong mga path sa script at stylesheet ng SweetAlert. Kung ang mga ito ay hindi tama, hindi ma-load ang library nang maayos.

3. Tsekahin ang iba pang mga script o error sa console: Kung may mga iba pang mga script na nagkakaroon ng error sa console, maaaring ito ang nagiging hadlang sa tamang pagpapakita ng SweetAlert. Tiyakin na wala kang ibang mga error sa iyong console ng developer tools.

4. Tignan ang mga network request: Sa developer tools, tingnan ang mga network request na ginagawa. Kung mayroong error sa iyong PHP script o kung hindi matagumpay ang request, maaaring ito ang dahilan kung bakit hindi nagpapakita ang SweetAlert. Tiyakin na ang iyong PHP script ay tama at gumagana nang maayos.

5. I-check ang mga dependenteng library: Siguraduhin na ang SweetAlert ay hindi umaasa sa iba pang mga library na hindi na-include o hindi ginagamit nang tama. Tiyakin na ang SweetAlert ay ang pangunahing script na ginagamit sa pagpapakita ng alert.

6. Tiyakin na naka-enable ang JavaScript: Siguraduhin na ang JavaScript ay naka-enable sa iyong browser. Kung hindi ito naka-enable, hindi gagana ang SweetAlert o anumang iba pang JavaScript-based na code.

7. Subukang i-debug ang iyong code: Kung sinusunod mo ang mga hakbang na ito nang tama at hindi pa rin nagpapakita ang SweetAlert, subukang i-debug ang iyong code. Gamitin ang console.log() upang ma-monitor ang mga data at matiyak na ang iyong script ay umaabot sa tamang bahagi.

Kung matapos subukan ang mga solusyon na ito ay hindi pa rin gumagana ang SweetAlert, maaaring may iba pang mga isyu o maling pagkakasulat sa iyong code. Maaaring mo itong ipost dito at tutulungan kita na hanapin ang mga problema.
 
Kapag hindi lumalabas ang SweetAlert, narito ang ilang mga posibleng dahilan at mga solusyon:

1. Siguraduhin na naka-include ang SweetAlert library: Tiyakin na naka-include mo ang tamang library ng SweetAlert sa iyong proyekto. Siguraduhin na mayroon kang link o script tag na naglalaman ng kinakailangang SweetAlert library na ito:

HTML:
<script src="path/to/sweetalert.min.js"></script>
<link rel="stylesheet" href="path/to/sweetalert.css">

2. Tiyaking tama ang iyong mga path: Siguraduhin na tamang ang iyong mga path sa script at stylesheet ng SweetAlert. Kung ang mga ito ay hindi tama, hindi ma-load ang library nang maayos.

3. Tsekahin ang iba pang mga script o error sa console: Kung may mga iba pang mga script na nagkakaroon ng error sa console, maaaring ito ang nagiging hadlang sa tamang pagpapakita ng SweetAlert. Tiyakin na wala kang ibang mga error sa iyong console ng developer tools.

4. Tignan ang mga network request: Sa developer tools, tingnan ang mga network request na ginagawa. Kung mayroong error sa iyong PHP script o kung hindi matagumpay ang request, maaaring ito ang dahilan kung bakit hindi nagpapakita ang SweetAlert. Tiyakin na ang iyong PHP script ay tama at gumagana nang maayos.

5. I-check ang mga dependenteng library: Siguraduhin na ang SweetAlert ay hindi umaasa sa iba pang mga library na hindi na-include o hindi ginagamit nang tama. Tiyakin na ang SweetAlert ay ang pangunahing script na ginagamit sa pagpapakita ng alert.

6. Tiyakin na naka-enable ang JavaScript: Siguraduhin na ang JavaScript ay naka-enable sa iyong browser. Kung hindi ito naka-enable, hindi gagana ang SweetAlert o anumang iba pang JavaScript-based na code.

7. Subukang i-debug ang iyong code: Kung sinusunod mo ang mga hakbang na ito nang tama at hindi pa rin nagpapakita ang SweetAlert, subukang i-debug ang iyong code. Gamitin ang console.log() upang ma-monitor ang mga data at matiyak na ang iyong script ay umaabot sa tamang bahagi.

Kung matapos subukan ang mga solusyon na ito ay hindi pa rin gumagana ang SweetAlert, maaaring may iba pang mga isyu o maling pagkakasulat sa iyong code. Maaaring mo itong ipost dito at tutulungan kita na hanapin ang mga problema.
salamat kailangan ko lang pala ibaba ang php codes
 
Back
Top