Skip to content Skip to sidebar Skip to footer

Update A Php Variable Within The Same Page Without Reloading Page

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. That variable will then be us

Solution 1:

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.


Post a Comment for "Update A Php Variable Within The Same Page Without Reloading Page"