Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php jquery函数未在页面加载时发送值_Php_Jquery_Ajax - Fatal编程技术网

Php jquery函数未在页面加载时发送值

Php jquery函数未在页面加载时发送值,php,jquery,ajax,Php,Jquery,Ajax,我正在从jQuery函数向我的主体发送一个值,但jQuery似乎没有发送该值。我想在加载页面时启动以下jQuery函数: <!DOCTYPE html> <html> <head> <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> <script type="t

我正在从jQuery函数向我的主体发送一个值,但jQuery似乎没有发送该值。我想在加载页面时启动以下jQuery函数:

 <!DOCTYPE html>
    <html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

  <script type="text/javascript" src='http://cdn.bitbucket.org/pellepim/jstimezonedetect/downloads/jstz-1.0.4.min.js'></script>
        <script>
            $(document).ready(function(){  
                var tz = jstz.determine(); // Determines the time zone of the browser client 
                jQuery('body').load('index.php?session_name='+tz.name());
            });
        </script>

    </head>
    <body>

$(文档).ready(函数(){
var tz=jstz.determinate();//确定浏览器客户端的时区
jQuery('body').load('index.php?session_name='+tz.name());
});
index.php:

<?php
session_start();

if (isset($_GET['session_name'])) {$_SESSION['session_name'] = $_GET['session_name'];} 
echo $_SESSION['session_name'];

PHP
GET
从URL获取值,因此当您使用
jQuery.load
时,页面有数据,但您的URL没有任何
GET
请求数据,因此,将永远不会设置会话

如果您想以这种方式设置会话,只需将页面重定向到您想要的URL,会话就会被设置

因此,与其这样做,不如:

jQuery('body').load('index.php?session_name='+tz.name())

重定向到具有get值的页面:

window.location='index.php?session_name='+tz.name()

使用jQuery更好的方法是将请求异步发送到页面,这样就不必重新加载页面

最简单的AJAX方式是:

$.ajax({
  type: "POST",
  url: "index.php",
  data: { session_name: tz.name() }
}).done(function( msg ) {
 // alert( "Session Saved: " + msg );
  console.log(msg);
});
和您的PHP:

if (isset($_POST['session_name'])) {$_SESSION['session_name'] = $_POST['session_name'];} 
echo $_SESSION['session_name'];

PHP
GET
从URL获取值,因此当您使用
jQuery.load
时,页面有数据,但您的URL没有任何
GET
请求数据,因此,将永远不会设置会话

如果您想以这种方式设置会话,只需将页面重定向到您想要的URL,会话就会被设置

因此,与其这样做,不如:

jQuery('body').load('index.php?session_name='+tz.name())

重定向到具有get值的页面:

window.location='index.php?session_name='+tz.name()

使用jQuery更好的方法是将请求异步发送到页面,这样就不必重新加载页面

最简单的AJAX方式是:

$.ajax({
  type: "POST",
  url: "index.php",
  data: { session_name: tz.name() }
}).done(function( msg ) {
 // alert( "Session Saved: " + msg );
  console.log(msg);
});
和您的PHP:

if (isset($_POST['session_name'])) {$_SESSION['session_name'] = $_POST['session_name'];} 
echo $_SESSION['session_name'];

您是否尝试过将该参数作为
POST
发送?应该是这样的:

$("body").load("index.php", {param1:value, param2:value, etc.})

您是否尝试过将该参数作为
POST
发送?应该是这样的:

$("body").load("index.php", {param1:value, param2:value, etc.})

你确定tz中有一个值吗。您可以放置一个警报语句并验证该值吗?尝试将您的代码放在
$(文档)之外。准备好了吗
@codeinzone我确信tz中有值。或者,即使我删除它并添加一些手动值,如果您发布的代码是整个
index.php
文件,它也不会工作,当然它不会工作-是这样吗?PHP GET从url获取值,因此当您执行JQuery.load时,页面具有数据,但您的url没有任何GET请求,因此会话将永远不会设置。您是否确保tz中有值。您可以放置一个警报语句并验证该值吗?尝试将您的代码放在
$(文档)之外。准备好了吗
@codeinzone我确信tz中有值。或者,即使我删除它并添加一些手动值,如果您发布的代码是整个
index.php
文件,它也不会工作,当然它不会工作-是吗?PHP GET从url获取值,因此当您执行JQuery.load时,页面具有数据,但您的url没有任何GET请求,因此会话将永远不会设置。我可以使用ajax更新您的答案,将请求发送到timestamp.PHP页面,在这个页面中,我将设置会话值。我可以使用ajax更新您的答案以将请求发送到timestamp.php页面,在这个页面中,我将设置会话值。