Change value of PHP variable with AJAX

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

The PHP:


The HTML:

4条回答
  •  青春惊慌失措
    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);
            });
    
        });
    
    });
    

提交回复
热议问题