这是我必须调用index.php文件的代码。
<script type="text/javascript">
$('button').click(function(){
$.ajax({url: 'permiteCookie.php'});
document.getElementById('div_cookies').style.display = 'none';
return false;
})
</script>
<?php
session_start();
if (!isset($_SESSION['permiteCookie'])){
?>
<div class="div_cookies" style="display: block;">
<div>
<div>
<div class="cookies_botoes">
<div class="cookies_bt2">
<button id="button" name="button" type="button">Close</button>
<a id="button" name="button" href="javascript:void(null);">Close</a></div>
</div>
</div>
</div>
</div>
<?php
}
?>
这是我在permiteCookie.php 文件中的代码
<?php
session_start();
$_SESSION['permiteCookie']=1;
?>
问题:我无法访问index.php文件中的$_SESSION['permiteCookie']。
我做错了什么?
脚本在DOM准备好之前就已经运行了。您需要将其封装在$(document).ready()
处理程序中:
<script type="text/javascript">
$(document).ready(function(){
$('button').click(function(){
$.ajax({url: 'permiteCookie.php'});
document.getElementById('div_cookies').style.display = 'none';
return false;
});
});
</script>
只要您使用的是jQuery,为什么要使用普通DOM来隐藏div?
$('#div_cookies').hide();
如果我没有错的话,因为session_start()会影响页眉,所以它需要位于页面的顶部,最好是第一行代码。
即,将该段添加到index.php的最顶部:
<?php
session_start();
if (!isset($_SESSION['permiteCookie'])){
?>