如何通过 Javascript 将 php 会话从一个页面转移到另一个页面?
我有一个表单(form.php),它在标题中使用以下JS代码:
<script type="text/javascript">
$(document).ready(function(){
$("a").click(function(){
$.post('test.php', function(data) {
$('#test').html(data);
});
});
});
</script>
A在form.php中有一个链接,一旦单击我想要一个下拉菜单出现(不刷新页面),例如“显示下拉菜单”。下拉列表中的代码在 test.php 中管理。
Test.php 从另一个服务中提取数据。为了获取此数据,我使用会话中保存的数据,例如 $_SESSION['data_that_is_sent_to_another_service']。
我在 form.php 中启动会话,但为了让 test.php 从其他服务获取信息,我需要在 test.php 开头启动会话。
该代码有效,但我收到一条警告:
Warning: session_start() [function.session-start]:
Cannot send session cache limiter - headers already sent
(output started at /..my directory.../test.php:1) in /...my directory.../hrdeals.php on line 4
如何才能不出现此警告(而不关闭 PHP 中的警告)?
非常感谢任何帮助。
Gregor
PS - JS 可能不正确,但我可以解决更多的 SESSION 问题
I have a form (form.php) that uses the following JS code in the header:
<script type="text/javascript">
$(document).ready(function(){
$("a").click(function(){
$.post('test.php', function(data) {
$('#test').html(data);
});
});
});
</script>
A have a link in form.php that once clicked I want a drop down to appear (without refreshing the page) e.g. "Display drop down". The code in the drop down is managed in test.php.
Test.php pulls data in from another service. In order to do get this data I use data that is kept in the session e.g. $_SESSION['data_that_is_sent_to_another_service'].
I start the session in form.php but in order for the test.php to get the information from the other service I need to start a session at the start of test.php.
The code works, but I then get a warning saying:
Warning: session_start() [function.session-start]:
Cannot send session cache limiter - headers already sent
(output started at /..my directory.../test.php:1) in /...my directory.../hrdeals.php on line 4
How can I get this warning not to appear (without turning off warnings in PHP)?
Any help much appreciated.
Gregor
PS - the JS might not be correct, but it's more the SESSION issue that I can solve
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在向浏览器输出任何内容之前,必须调用 session_start() 。检查 test.php 并确保没有输出。
session_start() must be called before outputing anything to the browser. Check test.php and make sure there is no output.
Javascript 和 PHP 会话彼此无关。 JavaScript 生活在它的小泡沫中(客户端渲染/处理)。 PHP 生活在它的泡沫(服务器端处理/输出)中。
您得到的错误是(简单地)- 标头已发送。
这是指 http headers(不是你可能有一些 javascript 的 head 标签)。您可以在此处查看完整列表:http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
当您在输出标题之前开始渲染网页内容时,会发生此错误。必须首先完成所有标题。
检查 php 的所有回显和打印是否在 session_start() 发生后发生。打印的一个棘手的地方是当你结束 php 标签并在那里有任何东西时 - 即使它位于页面的末尾。您不需要在页面末尾添加结束 php 标记,甚至后面的空格也会导致此类问题。最安全的方法是删除 php 文件底部的所有结尾 php 标签。
另一个棘手的事情是,这种错误也是由代码编辑器输出的“字节顺序标记”引起的。您可以尝试在简单的纯文本编辑器中打开、修复和保存页面,看看是否可以解决问题。
Javascript and PHP Sessions have nothing to do with one another. Javascript lives in its little bubble (client side rendering/processing). PHP lives in its bubble (server side processing/output).
The error you get is (briefly) - headers already sent.
This refers to http headers (not the head tag where you may have some javascript). You can see the whole list here: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
This error occurs when you start rendering web page stuff before outputting a header. All headers must be completed first.
Check that all echos and prints from php are occurring after session_start() occurs. One tricky place that printing creeps in is when you end php tags and have anything there - even when it's at the end of a page. You don't need ending php tags at the end of your pages and even a space following one will cause this kind of problem. The safest thing is to just remove all ending php tags at the bottom of your php files.
Another tricky thing is that this kind of error is also caused by a "Byte Order Mark" being output by your code editor. You can try opening, fixing and saving the page in just a simple plain-text editor and see if that fixes the problem.