Skip to content
Advertisement

How to check duplicate but without refresh page? PHP

this is my coding. I want to check the duplicate data. If duplicate data exists, the alert will pop-out, but once I click on OK, it will go to a blank page and cannot return to the form. How to make it pop-out an alert but without refresh the page?

<?php

include "db_con.php";
if (isset($_POST['edit_appt'])) {
    $appointment_id = $_POST['appointment_id'];
    $job_number = $_POST['job_number'];
    $technician_id = $_POST['technician_id'];
    $appointment_date = $_POST['appointment_date'];
    $appointment_time = $_POST['appointment_time'];

    $query = "SELECT * FROM appointment WHERE technician_id='$technician_id' && appointment_date='$appointment_date' && appointment_time='$appointment_time';";
    $result = mysqli_query($con, $query);
    if (mysqli_num_rows($result) == 1) {
        echo "<script>alert('Technician is unavailable! Please select another technician!!')</script>";
    } else {
        $query2 = "UPDATE appointment SET technician_id='$technician_id' WHERE appointment_id='$appointment_id'";
        $result2 = mysqli_query($con, $query2);
        if ($result2) {
            $query1 = "UPDATE reparation SET technician_id='$technician_id',notification_status='unread' WHERE job_number='$job_number'";
            $result1 = mysqli_query($con, $query1);
            if ($result1) {
                echo "<script>alert('Updated!')</script>";
                echo "<script>window.open('admin_appt.php','_self')</script>";
            }
        }
    }
}

Advertisement

Answer

One of the solutions is to do a javascript redirection.

Try changing

echo "<script>alert('Technician is unavailable! Please select another technician!!')</script>";

to

echo "<script>alert('Technician is unavailable! Please select another technician!!'); history.go(-1);</script>";

Please also consider using parameterized prepared statement to avoid SQL injection, as Strawberry has mentioned. (you may refer to this: How can prepared statements protect from SQL injection attacks?)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement