doukezi4606 2013-05-16 11:40
浏览 53
已采纳

使用ajax(相同页面)从jquery值更改php变量[关闭]

What I'm trying to do it simply this:

I've a php global variable $currentID and with a simple jquery script I check if there is a "id" (page) in the hash of url and if yes I wanted to give that value to $currentID. I've been reading a lot and I notice that I shall use Ajax but I'm not managing it.

My code now is:

<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>

Could you help me please? Thank you in advance.

  • 写回答

1条回答 默认 最新

  • duanjiongzhen2523 2013-05-16 11:50
    关注

    PHP is a server side language, meaning that once it has sent its data to the browser, the PHP variables and data cannot be changed...

    However, using AJAX, you can send another request to a PHP page and change data in the DOM based on the result.

    Consider this:

    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);
        }
    });
    

    The following will include the $currentID as it was sent when the page loaded... But, you can update this text using the above script.

    <p>My current ID is: <span id="id">$currentID</span></p>
    

    PLEASE NOTE: This will not update the PHP variable as once PHP has sent data to the browser, the script has terminated and it cannot be modified, nor can varibles be accessed. The above script will simply update the text in the HTML.

    Server Side Scripting

    You need to understand how server side scripting functions before you start programing PHP, it is pretty basic really:

    1. You send a request to my.php
    2. This request goes to the server where PHP generates your HTML, it will write any variables you have specified into the HTML
    3. The data is then sent back as HTML, NOT PHP, to the browser, where you can use javascript to make changes to the DOM.

    The bottom line is, once PHP has generated and sent your page data to the browser, nothing to do with PHP then exists on that page! It is pure HTML, or whatever other type of document it may be...

    Another way to explain it is this:

    Your server sees the PHP page as this:

    echo "<p>My current ID is: <span id=\"id\">$currentID</span></p>";
    

    Your browser (where javascript sees it), sees the page like this

    <p>My current ID is: <span id="id">1</span></p>
    

    There is no PHP variable to access, it simply does not exist!

    See the following for more information:

    https://softwareengineering.stackexchange.com/questions/171203/what-are-the-difference-between-server-side-and-client-side-programming

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?