弹出框javascript+;PHP代码混合不起作用?
我正在进行一个大型项目,其中包括一个带有cookie信息的弹出框,它只会在会话中第一次打开,但在javascript和PHP的混乱中,我似乎犯了一个错误或其他什么,因为它根本没有出现 下面是代码片段:弹出框javascript+;PHP代码混合不起作用?,javascript,php,html,css,session,Javascript,Php,Html,Css,Session,我正在进行一个大型项目,其中包括一个带有cookie信息的弹出框,它只会在会话中第一次打开,但在javascript和PHP的混乱中,我似乎犯了一个错误或其他什么,因为它根本没有出现 下面是代码片段: <div id='popup' style='border-radius: 1vw; display: hidden; background-color: white; padding: 2vw; position: fixed; top: 20%; right: 25%; z-index
<div id='popup' style='border-radius: 1vw; display: hidden; background-color: white; padding: 2vw; position: fixed; top: 20%; right: 25%; z-index: 999; width: 50vw; height: auto; margin-left: 15; margin-right: 15; border-style: solid; border-color: #181818; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);'>
<a class="sidebar" style="position:absolute; top:0 ;right: 1vw;font-size: 36px; margin-left: 50px;" #href="javascript:void(0)" class="closebtn" onclick="closePopUp()">×</a> <!-- The button which activates Javascript to close the pop up box -->
<img id='cookiepic' src='cookie.jpg' style='width: 3vw; height: 3vw; display: hidden;'>
<p class='spacedfont' style='display: inline' >   Oh, and by the way...</p>
<hr>
<div style='display: flex; flex-direction: row; display:hidden;' id='cookieinfo'>
<p id='cookiep' style='letter-spacing: 1px;'>
As is common practice with almost all professional websites, this site uses cookies, which are tiny files that are downloaded to your computer, to improve your experience.
<br><br>This page describes what information they gather, how we use it and why we sometimes need to store these cookies.
<br><br>We will also share how you can prevent these cookies from being stored however this may downgrade or 'break' certain elements of the sites functionality.
<br><br>By closing this notice or continuing to use the site, you consent to our use of cookies.
</p>
</div>
<div style='display: flex; flex-direction: row; display:hidden;' id='aboutinfo'>
<p id='aboutp'>
We're a fresh business to the world of e-commerce that aims to bring you the best experience shopping online.
<br><br>Our business is based in the UK but our web of business relationships stretches worldwide to deliver you the best service.
<br><br>Part of our ethos is innovation, a key role in our success which allows us to scour the globe for offers and deals to bring to you almost daily. Be sure to check the featured panel for a taster of what we've found recently!
<br><br>If you'd like to know anything else, email a member of our friendly team at xxxxx@gmail.com
</p>
</div>
</div>
<script>
function closePopUp() {
document.getElementById("popup").style.display = "none";
document.getElementById("cookieinfo").style.display = "none";
document.getElementById("cookiepic").style.display = "none";
document.getElementById("aboutinfo").style.display = "none";
}
function openPopUp() {
document.getElementById("popup").style.display = "block";
}
function showCookie() {
document.getElementById("popup").style.display = "block";
document.getElementById("cookieinfo").style.display = "inline";
document.getElementById("cookiepic").style.display = "inline";
<?php
$_SESSION['cookieconsent'] = true;
?>
}
function showAbout() {
document.getElementById("popup").style.display = "block";
document.getElementById("aboutinfo").style.display = "inline";
}
closePopUp();
<?php
if(empty($_SESSION['cookieconsent'])) {
echo "showCookie()";
}
?>
</script>
我们是电子商务领域的新业务,旨在为您带来最佳的在线购物体验。
我们的业务总部设在英国,但我们的业务关系网络遍布全球,为您提供最佳服务。
创新是我们精神的一部分,创新是我们成功的关键,它使我们能够在全球范围内搜寻几乎每天都能为您带来的优惠和交易。一定要检查功能面板的品酒师,我们最近发现了什么!
如果您还想了解其他信息,请发送电子邮件至xxxxx@gmail.com
函数closePopUp(){
document.getElementById(“弹出”).style.display=“无”;
document.getElementById(“cookieinfo”).style.display=“无”;
document.getElementById(“cookiepic”).style.display=“无”;
document.getElementById(“aboutinfo”).style.display=“无”;
}
函数openPopUp(){
document.getElementById(“弹出”).style.display=“块”;
}
函数showCookie(){
document.getElementById(“弹出”).style.display=“块”;
document.getElementById(“cookieinfo”).style.display=“inline”;
document.getElementById(“cookiepic”).style.display=“inline”;
}
函数showAbout(){
document.getElementById(“弹出”).style.display=“块”;
document.getElementById(“aboutinfo”).style.display=“inline”;
}
closePopUp();
有人知道怎么了吗?任何帮助都将不胜感激,作为记录,我更喜欢保持CSS内联。始终会执行
showCookie()
中的php代码(因为客户端javascript不会影响服务器端php代码)。因此,当您点击按钮时,
$\u会话['cookieconsent']
将始终被设置(因此,showCookie()
将永远不会被调用)
快速解决方案:
move
$\u会话['cookieconsent']=true代码>在回显“showCookie()”之后代码>:
showCookie()中的php代码
没有意义。它将一直被执行。因此,当您点击按钮时,$\u会话['cookieconsent']
将一直被设置(因此,showCookie()
将永远不会被调用),您将服务器端和客户端搞混了!嗨,欢迎来到StackOverflow!请编辑您的示例,使其仅包含与您的问题相关的代码。我们把这称为一场战争。这样,您也不必证明为什么不使用外部样式表/提示:除了在HTML中处理电子邮件模板外,没有理由不使用move$\u SESSION['cookieconsent']=true代码>在回显“showCookie()”之后代码>(但在if中)的可能副本
<?php
if(empty($_SESSION['cookieconsent'])) {
echo "showCookie()";
$_SESSION['cookieconsent'] = true;
}
?>