VitorTech
VitorTech

Reputation: 23

Confirm Box in PHP?

I'm developing a web application and I want to get the answer(yes or no) from user when the deactivate button is pressed. I want to ask him if you want or not to deactivate the account. I'm using PHP to set active status to = 1 when deactivated and 0 to activated.

I want to get the result and verify in PHP if the query runs or not.

I would appreciate if someone helps me, thanks. Here is the code:

 if(isset($_POST['desativar']))
{

  $login = $_SESSION['login'];


   mysqli_query($conexao,"UPDATE usuarios SET ativo_usuario = 1 WHERE login_usuario='$login'");

}

Upvotes: 0

Views: 14161

Answers (5)

Alexis
Alexis

Reputation: 5831

You can use onsubmit attribute.

Example

function confirmDesactiv()
{
   return confirm("Are you sure ?")
}
<form method="POST" action="yourphp.php" onsubmit="return confirmDesactiv()">
    <button type="submit">Delete my account</button>
</form>

Upvotes: 2

kiran gadhvi
kiran gadhvi

Reputation: 228

**JS**

    <script type="text/javascript">
    function confirmDesactiv()
    {
       var flag = confirm("Are you sure ?");
       if(flag)
            window.open("yourphp.php?id=" + 1);
        else
            window.open("yourphp.php?id=" + 0);
    }

</script>
**HTML**    



    <form method="POST" action="">
    <button type="submit" onclick="confirmDesactiv()">Desactiv my account</button>
    </form>



**PHP**   

<?php
echo $_GET['id'];
if(isset($_GET['id']) && $_GET['id'])
{
    //your update query will come here
}
?>                                                                                            

Upvotes: 0

user5282069
user5282069

Reputation:

If you don't want to use AJAX, and assuming your page is called index.php:

<?
   $deactivateStatusDisplay = "display: none"; // Initially, do not display a status.
   $deactivateStatusMsg = ""; //Status message is initialized as blank.
   $queryRan = false; // Just a flag to know if the user clicked deactivation link.

   if( 1 === $_GET['deactivate'] ){
      $deactivateStatusDisplay = "display: block";
      $deactivateStatusMsg = "Your account will be deactivated.";
      $queryRan = true;
   }
?>

<!DOCTYPE html >
<html>
     <head>
       <!-- ALL YOUR METADATA -->
     </head>
     <body>
         <div>Do you want to deactivate your account?</div>
         <a href="index.php?deactivate=1">DEACTIVATE</a>
         <div id="status" style="<? echo $deactivateStatusDisplay ?>"><? echo $deactivateStatusMsg ?></div>
     </body>
</html>

Upvotes: 0

Peter
Peter

Reputation: 9113

You can use PHP to make something like a confirmation box. Below is a little smaple code to show you how:

<?php
session_start();

if (isset($_POST['submit'])) {
    $_SESSION['postdata'] = $_POST;
    header("Location: ".$_SERVER['PHP_SELF']);
    exit;
} else if (isset($_POST['confirm']) && $_POST['confirm'] == 'yes') {
    // Do stuff that should only be done after confirming
} elseif (isset($_POST['confirm']) && $_POST['confirm'] == 'no') {
    // Cancelled, redirect back to page
    header("Location: ".$_SERVER['PHP_SELF']);
    exit;
}
?>

<?php if (isset($_SESSION['postdata']) && !isset($_POST['confirm'])): ?>
    <form method="POST" action="<?= $_SERVER['PHP_SELF']; ?>">
        Are you sure you want to do this?<br />
        <input type="submit" name="confirm" value="yes">
        <input type="submit" name="confirm" value="no">
    </form>
<?php else: ?>
    <!-- Your form here -->
<?php endif; ?>

Upvotes: 0

Keyur Chavda-kc1994
Keyur Chavda-kc1994

Reputation: 1045

try it.

function confirm_delete(){
    if(confirm("Are you sure you want to delete this..?") === true){
        return true;
    }else{
        return false;
   }
 }

 <input type="button" Onclick="confirm_delete()">

Upvotes: 0

Related Questions