PHP中的确认框?

bwitn5fc  于 2023-02-15  发布在  PHP
关注(0)|答案(5)|浏览(163)

我正在开发一个网络应用程序,我想得到用户的答案(是或否)时,停用按钮被按下。我想问他,如果你想或不停用帐户。我用PHP设置活动状态为= 1时停用和0激活。
我想得到结果,并在PHP中验证查询是否运行。
如果有人能帮我,我会很感激,谢谢。下面是代码:

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

  $login = $_SESSION['login'];

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

}
xeufq47z

xeufq47z1#

您可以使用onsubmit属性。

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

smdncfj32#

试试看。

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()">
x4shl7ld

x4shl7ld3#

你可以用PHP来做一个类似确认框的东西。下面是一个简单的代码来告诉你如何做:

<?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; ?>
ajsxfq5m

ajsxfq5m4#

如果你不想使用 AJAX ,并假设你的页面名为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>
igsr9ssn

igsr9ssn5#

**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
}
?>

相关问题