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

报告相同问题?

悬赏问题

  • ¥200 如何使用postGis实现最短领规划?
  • ¥15 pyinstaller打包错误
  • ¥20 cesm的气溶胶排放文件
  • ¥15 逐月累计,月份不连续,补齐月份
  • ¥15 应用简单的Python代码完成一个学生成绩管理系统
  • ¥15 用matlab求微分方程初值问题
  • ¥15 vscode下编写第三方库opencv与pcl代码时没有代码提示
  • ¥15 能够跑通不报错,如何解决?(标签-matlab)
  • ¥15 MOS在RDS较大,频率高时开关波形异常
  • ¥15 SCENIC分析报错求解答