使用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?

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>
<script type="text/javascript>

    function changePage(pageDest){
    var xmlobject = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");

        xmlobject.onreadystatechange = function (){
            if(xmlobject.readyState == 4 && xmlobject.status == 200){
                document.getElementById("mainContent").innerHTML = xmlobject.responseText;
            }
            else{
                document.getElementById("mainContent").innerHTML = 'Loading...';
            }
        }
        xmlobject.open("GET",pageDest,true);
        xmlobject.send();
    }

</script> 



<div class="mainContent" id="mainContent">
    Change this HTML
</div>

<div onmouseup="changePage('views/dashboardView.php')">Get dashboard view</div>

The parameter in the changePage function is the location of the page that you would like to place in your mainContent <div>

Does this help?

You cannot change the value of a PHP variable, as PHP is Server Side (done first), and JS is Client Side (done after Server Side).

Typically AJAX is used to repopulate an area of a web page, but that would suit your purpose. In the example below, ajax/test.php is the new file you want to include. Obviously change the path/name as you wish, and create that file.

I will add though, if you are repopulating a large chunk of your page, it will probably be just as quick to fully reload it.

$(function(){

    $('.your-button-class').on('click', function(){

        $.post('ajax/test.php', function(data) {
            $('.mainContent').html(data);
        });

    });

});

Storing the View in the session, will keep the site displaying this view until the user closes the browser and ends the session, the session expires or they change the view again.

The include that sets mainView

<?php
    session_start();
    $mainView = "views/dashboardView.php";  // default
    if(isset($_SESSION['mainView']))
    {
        $mainView =$_SESSION['mainView'];
    }
?>

// the ajax script that sets the mainView

<?php
    session_start();
    $_SESSION['mainView']='views/'.$_GET['mainView'].'.php';
?>

the javascript link for ajax

ajaxURL='ajax.php?mainView=otherDasboard';

you may also want to check for empty session variable and that the file exists before setting it