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

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

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路