douting1871 2012-11-02 14:12
浏览 147
已采纳

使用AJAX更改PHP变量的值

The PHP:

<?php

$mainView = "views/dashboardView.php";

?>

The HTML:

<div class="mainContent">
    <?php include($mainView); ?>
</div>

I would like the click event of a button to change what view .mainContent shows and I believe AJAX can accomplish this but as yet have not been able to get it to work.

Any advice?

  • 写回答

4条回答 默认 最新

  • dongyong6332 2012-11-02 14:28
    关注

    You would have to modify your PHP script to allow for this.

    For example:

    PHP:

    if (isset($_POST['change']))
    {
        $mainView = $_POST['change'];
        echo $mainView;
    }
    

    HTML & jQuery:

    <button id="change">Change the var</button>
    <script>
    $("#change").click(function() {
        $.post("file.php", {change: $(this).val()},
            function (data)
            {
               $("#mainContent").html(data);
            });
    });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?