Update a php variable within the same page without reloading page

后端 未结 1 1680
借酒劲吻你
借酒劲吻你 2021-01-16 00:24

I am not very knowledgable with AJAX and I am pretty certain it\'s the solution to my problem.

I want to update a php variable on a onclick.

相关标签:
1条回答
  • 2021-01-16 01:17

    It is not possible to change a PHP variable without reloading and whihout using something like ajax to call a php file.

    The reason that it is not possible is that PHP is running Server side, and without a reload you can only change things on the Client side. If it was possible to change PHP variables on the Client side, this would cause a huge security problem.

    Updating a page using PHP code can only be done by sending a new request to the server, so it can execute the PHP code and send back whatever it outputs. Using ajax you can make these kind of requests in the background, and therefor "change the page without reloading", the reloading actually happens, but in the background and only the relevant part of the foreground page than needs to be updated using javascript.

    Example ajax solution:

    onclick: (replace 1 with the value you want to store, or with a var)

    $.get("storeSort.php", {sort:1}, function(data){
        //nothing to be done. is just send
    });
    

    create a storeSort.php with:

    <?php
    // Start the session
    session_start();
    
    // Set session variables
    $_SESSION["sort"] = $_GET['sort'];
    

    And use session_start() and the value of $_SESSION['sort'] in your page script to determine which jquery function to call.

    0 讨论(0)
提交回复
热议问题