简体   繁体   中英

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 <TH> onclick. That variable will then be used to sort some columns of mine using a Jquery table sorting script.

At the top of the page ( bodyshops.php ) I have this...

if (isset($_GET['sortStore'])) {
    $sortStore = $_GET['sortStore'];
}else{
    $sortStore = 'EMPTY';
}

at the table end I have this... (I put $sortStore = 'X' as an example)

<th style="text-align:left; cursor:pointer; background-color:#0a639d; color:#ffffff;" onclick="<?php $sortStore = '1' ?>"><?php echo $LANGT_Bodyshop_Name; ?></th>
<th style="cursor:pointer; background-color:#0a639d; color:#ffffff;" onclick="<?php $sortStore = '1' ?>">&nbsp;<?php echo $LANGT_Bodyshop_Contact; ?></th>
<th style="cursor:pointer; background-color:#0a639d; color:#ffffff;" onclick="<?php $sortStore = '2' ?>">&nbsp;<?php echo $LANGT_Bodyshop_Email; ?>&nbsp;</th>
<th style="cursor:pointer; background-color:#0a639d; color:#ffffff;" onclick="<?php $sortStore = '3' ?>">&nbsp;<?php echo $LANGT_Bodyshop_Phone; ?>&nbsp;</th>
<th style="cursor:pointer; background-color:#0a639d; color:#ffffff;" onclick="<?php $sortStore = '4' ?>">&nbsp;<?php echo $LANGT_Bodyshop_AddressPostcode; ?>&nbsp;</th>
<th style="cursor:pointer; background-color:#0a639d; color:#ffffff;" onclick="<?php $sortStore = '5' ?>">&nbsp;<?php echo $LANGT_Bodyshop_Capacity; ?>&nbsp;</th>
<th style="cursor:pointer; background-color:#0a639d; color:#ffffff;" onclick="<?php $sortStore = '6' ?>">&nbsp;<?php echo $LANGT_Bodyshop_CourtesyCars; ?>&nbsp;</th>

The end goal is to be able to write up an if statement to decide what jQuery script to run.

ie

if ( $sortStore != 0 ) {
    // Run Function 1 
}else{
    // Run Function 2
}

This would ultimately help me stopping TableSorter losing it's sort value when refreshing the page. Can anyone help?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM