weixin_33708432 2016-12-27 09:46 采纳率: 0%
浏览 9

通过Ajax更新数据

I have the below code on href click I'm calling a javascript code in which an ajax is being called which returns the value of array $ss in json format . Now I want to know how can I update the value of $ss via ajax.

 <div class="white" id="white" style="display:none">   
        <?php
            foreach ($ss as $key => $value){
        ?>
        <a  href='javascript:void(0);'  onclick='callAjax('<?php echo $key; ?>')'>
        <?php   
           echo $value;
        ?>
        </a>
        <?php
           }
        ?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
        var res;
        function on(id){
            //alert('hi '+id);
            $.ajax({
                url: 'ajax.php', //This is the current doc
                type: "GET",
                data: ({a: id }),
                success: function(data){
                    res = data;
                    //alert(res);
                    document.write(res);
                }
            }); 

        }

    </script>

And ajax.php files return array values for $ss. I understood how to update data of normal div through ajax but encountering problem to pass the data returned by ajax call to update array value.

  • 写回答

3条回答 默认 最新

  • weixin_33671935 2016-12-27 09:51
    关注

    1- Lets be clear javascript will not replace the content of a php variable.
    2- Even if so you will not be able to regenerate the html you need this way.
    3- after you receive your variable you need to update the html instead.

    PS: Surely you can update the variable on the server side when you receive that ajax call,
    that also needs some requirements(the variable is global and accessible inside the php file...),
    but from what I have understood you want to change it with javascript which is not possible.

    评论

报告相同问题?