简体   繁体   English

如何通过 HTML 代码中的 if 条件触发 javascript 弹出框?

[英]How to trigger javascript pop-up box by if-condition within the HTML code?

I created an html site where the user can input a code in a text input and then this code is being inserted in a MYSQL database if certain conditions are met.我创建了一个 html 站点,用户可以在其中输入文本输入中的代码,然后如果满足某些条件,则将此代码插入到 MYSQL 数据库中。 In case of a successfull database entry I would like to notify the user with a little pop up message, which displays something like "Success" and also stating some data from the table row, the code was inserted into.如果数据库条目成功,我想通过一个小的弹出消息通知用户,该消息显示“成功”之类的内容,并说明表行中的一些数据,代码已插入。
I found a nice looking pop up message on the following page, which I would like to use: https://www.gitto.tech/posts/animated-modal-box-using-html-css-and-javascript/ .我在以下页面上发现了一个漂亮的弹出消息,我想使用它: https : //www.gitto.tech/posts/animated-modal-box-using-html-css-and-javascript/
However, in the implementation from the page, the pop-up is triggered by the click of a button:但是,在从页面实现中,弹出窗口是通过单击按钮触发的:

  document.getElementById("open-popup-btn").addEventListener("click",function(){
  document.getElementsByClassName("popup")[0].classList.add("active");
});
 
document.getElementById("dismiss-popup-btn").addEventListener("click",function(){
document.getElementsByClassName("popup")[0].classList.remove("active");
});

I would like to execute the pop-up based on an if-condition, in which I check whether an php variable is already set:我想根据 if 条件执行弹出窗口,在其中检查是否已设置 php 变量:

<?php 
     if (isset($group)) {
?>    ......HTML code.....

So, can anybody tell me how to successfully remove that "onClick" function of the pop-up?那么,谁能告诉我如何成功删除弹出窗口的“onClick”功能?

Thanks in advance!提前致谢!

I understand that you reload the page after said data was inserted into database?我知道您在将数据插入数据库后重新加载页面?

If so, what you can do to show pop-up initially, is to simply add class active to your popup (element with class popup , like that:如果是这样,您最初可以做什么来显示弹出窗口,只需将类active添加到您的弹出窗口(具有类popup元素,如下所示:

<div class="popup center active">
  ...
</div>

BUT

in order to be able to close the popup, you still will have to attach the second part of the provided script (you can simply insert it within script tags inside php if statement (at the end of HTML body element), like:为了能够关闭弹出窗口,您仍然需要附加所提供脚本的第二部分(您可以简单地将其插入到 php if语句内的script标签中(在 HTML body元素的末尾),例如:

<script>
  document.getElementById("dismiss-popup-btn").addEventListener("click",function(){
    document.getElementsByClassName("popup")[0].classList.remove("active");
  });
</script>

note: It doesn't change the fact that, as said above - it would be better to handle this withXHR (eg via ajax)注意:它不会改变事实,如上所述 - 最好用XHR处理这个问题(例如通过 ajax)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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