简体   繁体   中英

php alert box with ok and cancel buttons

I have a php code (little bit javascript as well) as shown below in which which an alert message is displayed when second user tries to login the form with one user already logged in .

The issue which I am having right now is on clicking ok in the alert box , it goes inside the if block but it on clicking cancel it doesn't go inside the else block instead it goes inside the if block again .

<?php
    $message = "user " . $row['user_name'] . " is logged in. Do you want to take over ?";
    echo "<script type='text/javascript'>confirm('$message');</script>";
    if(confirm)
    {
        $stmt1=$connect->prepare("UPDATE trace_users SET write_access='0' WHERE write_access='1'"); // revoke write access of all users
        $stmt1->execute();
    }
    else
    {
        echo "I am in else block";  // Line A
        $stmt1=$connect->prepare("UPDATE trace_users SET write_access='1' WHERE write_access='0'"); // revoke write access of all users
        $stmt1->execute();
    }
?>

I have added Line A/Line B for debugging purpose. On clicking button Ok button Line A is called and on clicking Cancel button, Line A is called again.

Here's a quick example to get you started:

index.php
<?php
//....
$user =  "John Doe"; // $row['user_name'];
$message = "User $user is logged in. Do you want to take over?";

if (isset($_GET['takeover'])) {
    if ($_GET['takeover'] == "confirm") {
        // $stmt1 = $connect->pre......etc
        exit(json_encode(["message" => "Access confirmed"])); // Send response to JS
    } else {
        // $stmt1 = $connect->pre......etc
        exit(json_encode(["message" => "Access revoked"])); // Send response to JS
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>TEST</title>
</head>
<body>

  <h1>MY USER INTERFACE</h1>

  <script>
    const takeover = confirm("<?= $message ?>");
    fetch(`?takeover=${takeover ? "confirm" : "revoke"}`)
      .then(response => response.json())
      .then(data => {
        console.log(data);
      });
  </script>
</body>
</html>
  • the server will "strip" PHP tags before serving the page, but the echoed values will remain. Ie: $message inside the JavaScript code.
  • On Document ready the confirm() triggers, and a boolean value is carried in JS.
  • Depending on that value simply fetch the same file (or any other if you want) using a GET request somepagename.php?takeover=confirm
  • Listen in PHP for $_GET requests and respond to the AJAX request with a JSON response.
  • Open console, and see the server respond with the adequate message object data.

Happy coding.

On your userpage.php you have the Log In:

<a href="confirm_login.php?user_name=" . $row['user_name']>Log In</a>

In your confirm_login.php you have the confirmation with the two options:

$message = "user " . $_GET[user_name] . " is logged in. Do you want to take over ?";
<a href="userpage.php?confirm=yes">Yes</a>
<a href="userpage.php?confirm=no">No</a>

Now when you choose option you redirect back to userpage.php:

if ( $_GET[confirm] == "yes" ) {
    $stmt1=$connect->prepare("UPDATE trace_users SET write_access='0' WHEREwrite_access='1'");
    $stmt1->execute();
} else if ( $_GET[confirm] == "no" ) {
    $stmt1=$connect->prepare("UPDATE trace_users SET write_access='1' WHERE write_access='0'"); // revoke write access of all users
    $stmt1->execute();  
}

This is the plain PHP method described in your article. As other cool guys say you can use AJAX to not change URL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM