twitter-bootstrap 使用 php 触发器切换 Bootstrap 模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19177691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 18:43:39  来源:igfitidea点击:

Toggle Bootstrap modal with php trigger

javascriptphpjqueryjsontwitter-bootstrap

提问by justanotherhobbyist

I need to trigger a Bootstrap modal if a PHP GET url is set but I don't know how to.

如果设置了 PHP GET url,我需要触发 Bootstrap 模式,但我不知道如何操作。

The PHP code look's like this:

PHP 代码如下所示:

<?php
if ($_GET['activate']) {
$activate = $_GET['activate'];
    mysql_query("UPDATE `users` SET `active` = 1 WHERE `email_code` = '$activate'");
}
?>

After the mysql query but still in the if I need the modal to trigger. I've considered json but don't know enough to create my own code. The javascript to show the modal is this line:

在 mysql 查询之后但仍然在如果我需要模式触发。我已经考虑过 json 但不够了解来创建我自己的代码。显示模态的 javascript 是这一行:

$('#myModal').modal('show')

How would I translate that to work with my PHP trigger?

我将如何将其转换为与我的 PHP 触发器一起使用?

回答by Patsy Issa

Add a variable that checks the state,

添加一个检查状态的变量,

$show_modal = false;

And in your get set it to true,

在你的 get 中将它设置为 true,

if ($_GET['activate']) {
    $activate = $_GET['activate'];
    mysql_query("UPDATE `users` SET `active` = 1 WHERE `email_code` = '$activate'");
    $show_modal = true;
}

Now in your html :

现在在你的 html 中:

<?php if($show_modal):?>
  <script> $('#myModal').modal('show');</script>
<?php endif;?>

回答by Mike Oram

This is actually easier than most people realise, just inject your JS call into the page using a PHP if statement:

这实际上比大多数人意识到的要容易,只需使用 PHP if 语句将您的 JS 调用注入页面:

<?php if (($_GET['activate'])) { ?>
    <script type="text/javascript"> $('#myModal').modal('show'); </script>
<?php } ?>

Put this php if statement into the head of your document (or the footer if your considering load times) and it will open your modal as required.

将此 php if 语句放入文档的头部(或如果您考虑加载时间的页脚),它将根据需要打开您的模式。