Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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
使用ajax从jquery值更改php变量(同一页)_Php_Jquery_Ajax - Fatal编程技术网

使用ajax从jquery值更改php变量(同一页)

使用ajax从jquery值更改php变量(同一页),php,jquery,ajax,Php,Jquery,Ajax,我想做的只是: 我有一个php全局变量$currentID,通过一个简单的jquery脚本,我检查url哈希中是否有“id”(页面),如果是,我想将该值赋给$currentID。 我读了很多书,我注意到我将使用Ajax,但我没有管理它 我现在的代码是: <script type="text/javascript"> $(document).ready(function(){ if (parent.location.hash != '' ) { var theCurre

我想做的只是:

我有一个php全局变量
$currentID
,通过一个简单的jquery脚本,我检查url哈希中是否有“id”(页面),如果是,我想将该值赋给
$currentID
。 我读了很多书,我注意到我将使用Ajax,但我没有管理它

我现在的代码是:

<script type="text/javascript">
$(document).ready(function(){
   if (parent.location.hash != '' ) {
     var theCurrentID = parent.location.hash.split('#')[1];
      $.ajax({
            url: 'my.php', //current page
            type: 'post',
            data: {theID: theCurrentID },
            datatype: 'json',
            success: function () {

            }
        });
   }
  // your code here
});
</script>

$(文档).ready(函数(){
if(parent.location.hash!=''){
var theCurrentID=parent.location.hash.split(“#”)[1];
$.ajax({
url:'my.php',//当前页面
键入:“post”,
数据:{theID:theCurrentID},
数据类型:“json”,
成功:函数(){
}
});
}
//你的代码在这里
});

你能帮我吗?提前感谢。

PHP是一种服务器端语言,这意味着一旦将数据发送到浏览器,PHP变量和数据就无法更改

但是,使用AJAX,您可以向PHP页面发送另一个请求,并根据结果更改DOM中的数据

考虑这一点:

var id = parent.location.hash.split('#')[1];;

$.ajax({
    url: 'http://www.example.net/id.php',
    dataType: 'JSON',
    data: {id: id},
    success: function(data){
        // Do whatever you want with you JSON object
        // The following will log what has been returned
        console.log(data);
        // If you want to update your id variable then simply do this
        id = data.id;
        // You can then change elements in the DOM that reflect what your PHP
        // variable originally set
        $('#id').text(id);
    }
});
以下内容将包括加载页面时发送的
$currentID
。。。但是,您可以使用上述脚本更新此文本

<p>My current ID is: <span id="id">$currentID</span></p>
您的浏览器(javascript可以看到的地方)看到的页面如下所示

<p>My current ID is: <span id="id">1</span></p>
我当前的ID是:1

没有可访问的PHP变量,它根本不存在

有关更多信息,请参见以下内容:


PHP是一种服务器端语言,这意味着一旦它将数据发送到浏览器,PHP变量和数据就无法更改

但是,使用AJAX,您可以向PHP页面发送另一个请求,并根据结果更改DOM中的数据

考虑这一点:

var id = parent.location.hash.split('#')[1];;

$.ajax({
    url: 'http://www.example.net/id.php',
    dataType: 'JSON',
    data: {id: id},
    success: function(data){
        // Do whatever you want with you JSON object
        // The following will log what has been returned
        console.log(data);
        // If you want to update your id variable then simply do this
        id = data.id;
        // You can then change elements in the DOM that reflect what your PHP
        // variable originally set
        $('#id').text(id);
    }
});
以下内容将包括加载页面时发送的
$currentID
。。。但是,您可以使用上述脚本更新此文本

<p>My current ID is: <span id="id">$currentID</span></p>
您的浏览器(javascript可以看到的地方)看到的页面如下所示

<p>My current ID is: <span id="id">1</span></p>
我当前的ID是:1

没有可访问的PHP变量,它根本不存在

有关更多信息,请参见以下内容:



您无法更改服务器端变量客户端!PHP是一种服务器端脚本语言,因此一旦处理并发送到浏览器,就不能对PHP变量进行任何更改。但是,使用ajax,您可以从PHP页面提取数据,并在必要时更新HTML文档。PHP在服务器端进行处理。呈现htm后,除非重新加载页面,否则无法修改“嵌入的”php变量。也就是说,您可以设置一个由php变量设置的js变量。@user2389750清除您的要求:您想从服务器获取数据,还是通过ajax将数据设置到服务器!!!谢谢你们,我会看看你们的建议。Niladri Das,我只想能够在同一页面中使用jquery值修改php变量,而无需重新加载。您无法更改服务器端变量客户端!PHP是一种服务器端脚本语言,因此一旦处理并发送到浏览器,就不能对PHP变量进行任何更改。但是,使用ajax,您可以从PHP页面提取数据,并在必要时更新HTML文档。PHP在服务器端进行处理。呈现htm后,除非重新加载页面,否则无法修改“嵌入的”php变量。也就是说,您可以设置一个由php变量设置的js变量。@user2389750清除您的要求:您想从服务器获取数据,还是通过ajax将数据设置到服务器!!!谢谢你们,我会尝试看看你们的建议。Niladri Das,我只想能够在同一页面中使用jquery值修改php变量,而无需重新加载。在任何地方都不存在了…谢谢Ben!因此,无法获取id的值并将$currentID变量值更新为id?@user2389750。您需要了解服务器端脚本是如何工作的。PHP脚本在服务器上执行,然后生成的数据(不包含任何PHP变量,仅包含文本)将发送到浏览器(客户端)<代码>$currentID一旦生成页面就不存在!我会尽量用我的语言更好地解释它answer@user2389750您没有抓住要点,脚本完成/页面加载后没有
$currentID
变量。@jeroen看到我的更新,我试图用最简单的术语解释服务器端脚本是如何工作的。请随意添加:-)在任何地方都不存在了…谢谢本!因此,无法获取id的值并将$currentID变量值更新为id?@user2389750。您需要了解服务器端脚本是如何工作的。PHP脚本在服务器上执行,然后生成的数据(不包含任何PHP变量,仅包含文本)将发送到浏览器(客户端)<代码>$currentID一旦生成页面就不存在!我会尽量用我的语言更好地解释它answer@user2389750您没有抓住要点,脚本完成/页面加载后没有
$currentID
变量。@jeroen看到我的更新,我试图用最简单的术语解释服务器端脚本是如何工作的。请随意添加:-)