在同一页面内更新php变量,而无需重新加载页面 [英] Update a php variable within the same page without reloading page

查看:80
本文介绍了在同一页面内更新php变量,而无需重新加载页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太了解AJAX,并且可以肯定这是解决我的问题的方法.

我想在<TH> onclick上更新一个php变量.然后,该变量将用于通过Jquery表排序脚本对我的某些列进行排序.

在页面(bodyshops.php)的顶部,我有这个...

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

在桌子末端我有这个...(我以$sortStore = 'X'为例)

<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>

最终目标是能够编写一条if语句来确定要运行的jQuery脚本.

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

这最终将有助于我在刷新页面时阻止TableSorter失去其排序值.有人可以帮忙吗?

解决方案

如果不重新加载和使用ajax之类的东西来调用php文件,则无法更改PHP变量.

不可能的原因是PHP正在运行服务器端,并且没有重新加载,您只能在客户端上进行更改.如果可以在客户端上更改PHP变量,则将导致巨大的安全性问题.

使用PHP代码更新页面只能通过向服务器发送新请求来完成,因此它可以执行PHP代码并发回所有输出.使用ajax,您可以在后台发出这类请求,因此无需重新加载即可更改页面",实际上是在重新加载,但是在后台,仅前景页面的相关部分不需要使用javascript进行更新. /p>

ajax解决方案示例:

onclick :(用您要存储的值或var替换1)

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

创建具有以下内容的storeSort.php

:

<?php
// Start the session
session_start();

// Set session variables
$_SESSION["sort"] = $_GET['sort'];

然后在页面脚本中使用session_start()$_SESSION['sort']的值来确定要调用的jquery函数.

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.

i.e.

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.

这篇关于在同一页面内更新php变量,而无需重新加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆