doukezi4606
doukezi4606
2013-05-16 11:40

使用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 duanjiongzhen2523 8年前

    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

    点赞 评论 复制链接分享