Change value of PHP variable with AJAX

后端 未结 4 517
刺人心
刺人心 2021-01-24 09:11

The PHP:


The HTML:

相关标签:
4条回答
  • 2021-01-24 09:44

    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

    0 讨论(0)
  • 2021-01-24 09:45
    <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?

    0 讨论(0)
  • 2021-01-24 09:47

    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>
    
    0 讨论(0)
  • 2021-01-24 09:47

    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);
            });
    
        });
    
    });
    
    0 讨论(0)
提交回复
热议问题