不读取使用jquery-session变量调用php文件
这是我必须调用index.php文件的代码不读取使用jquery-session变量调用php文件,php,jquery,session-variables,Php,Jquery,Session Variables,这是我必须调用index.php文件的代码 <script type="text/javascript"> $('button').click(function(){ $.ajax({url: 'permiteCookie.php'}); document.getElementById('div_cookies').style.display = 'none'; return false; }) </script> <?php sessi
<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
}
?>
$(“按钮”)。单击(函数(){
$.ajax({url:'permitechookie.php'});
document.getElementById('div_cookies')。style.display='none';
返回false;
})
关
这是我在permitechookie.php文件中的代码
<?php
session_start();
$_SESSION['permiteCookie']=1;
?>
问题:我无法访问index.php文件中的$_会话['permiteCookie']
<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
}
?>
我做错了什么?在DOM准备就绪之前脚本正在运行。您需要将其包装在
$(文档)中。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>
如果我没有弄错的话,因为session_start()会影响页面标题,所以它需要位于页面顶部,最好是第一行代码 i、 e.将此段添加到index.php的最顶端:
<?php
session_start();
if (!isset($_SESSION['permiteCookie'])){
?>
您是否从index.php调用了session\u start()
?是的,它出现在index.php中。不过,我将在示例代码中纠正这一点……您必须执行两个单独的ajax操作。一个是启动$\u会话变量,另一个是基于$\u会话变量生成HTML。或者如果你只是想做一些与php相关的事情,通常只包含permitCookie.php。尝试了这个,仍然不起作用。即使隐藏div也不能正常工作。使用firebug,我在javascript控制台中看不到任何错误。有什么线索吗?检查firebug,是否提出了ajax请求?ajax请求是否发送正确的cookie头(两个请求都作为响应)?你在用COR吗?