通过jquery click事件在php中设置会话变量 [英] Setting session variable in php through jquery click event
问题描述
因此,到目前为止,我一直在研究Stackoverflow上的所有可能答案.但是,无论我如何尝试,我都无法正常工作.
So I have been looking at all possible answers so far on Stackoverflow. But no matter how I try, I can't get this to work.
我正在尝试根据点击的链接设置一个会话变量.
I'm trying to set a sessionvariable according to a clicked linked.
这可以是网格"或列表"
this can be 'grid' or 'list'
我可以更改类以及相应div的视图.但是,我对php的$ .post调用未传递该变量(我假设这样做,因为什么也没有发生),或者我的php调用是错误的.
I can change the class, and the view of the corresponding div. However, my $.post call to php is not passing the variable ( im assuming this, since nothing happens ) or my php call is wrong.
这是我的代码:
HTML
<li class="display-control-list-item" > <a href="javascript: void(0)" class="js-set-display list" id="list" > Listview </a> </li>
<li class="display-control-list-item" > <a href="javascript: void(0)" class="js-set-display grid" id="grid" > Gridview </a> </li>
JQUERY
function set_display_type( type ) {
if ( type == 'grid' ) {
$( '.js-set-display.list' ).removeClass( 'active' );
$( '.js-set-display.grid' ).addClass( 'active' );
$( '.content-inner' ).attr({ 'class' : 'container content-inner gridview' });
var view = 'gridview';
$.post( 'library/fx.behaviour.php' , { setdisplay: view });
}
if ( type == 'list' ) {
$( '.js-set-display.grid' ).removeClass( 'active' );
$( '.js-set-display.list' ).addClass( 'active' );
$( '.content-inner' ).attr({ 'class' : 'container content-inner listview' });
var view = 'listview';
$.post( 'library/fx.behaviour.php' , { setdisplay: view });
}
}
和我在fx.behaviour.php中的PHP
and my PHP in fx.behaviour.php
$_SESSION[ 'display' ] = 'listview'; // set default
if ( $_POST[ 'setdisplay' ] != '' ) {
$view = $_POST[ 'setdisplay' ];
$_SESSION[ 'display' ] = $view;
}
简而言之:单击URL并将值通过jquery传递给php后,sessionvariables将不会更新.
In short: the sessionvariable won't update after clicking the url and passing the value through jquery to php.
推荐答案
这就是我要做的(为了快速起见,切掉了一些代码):
Here's what I would do (cut some code out for quickness):
HTML(如果使用jQuery,则不需要javascript无效代码):
HTML (you don't require javascript void code if using jQuery):
<ul id="set-display">
<li><a href="" id="list">Listview</a></li>
<li><a href="" id="grid">Gridview</a></li>
</ul>
jQuery:
$('#set-display a').click(function(e) {
var type = $(this).attr('id');
var view = (type === 'grid') ? 'gridview' : 'listview';
$.post('library/fx.behaviour.php', { display: view });
// you can use the view or type variable to trigger your other code
});
PHP:
session_start(); // if not called already
// if POST display not empty and is expected string then use it, otherwise default to 'listview'
$_SESSION['display'] = ( ! empty($_POST['display']) && in_array($_POST['display'], array('listview', 'gridview'))) ? $_POST['display'] : 'listview';
我使用$_POST['display']
而不是setdisplay
来保持一致性.检查您的jQuery调用是否获得有效的200响应也可能是值得的.在Chrome中,您可以通过以下步骤进行操作:转到检查器,选择网络"标签,点击按钮,然后查看请求.
I used $_POST['display']
rather than setdisplay
for naming consistency. It might also be worth checking your jQuery call gets a valid 200 response. In Chrome you can do this by going to the Inspector, selecting the network tab, click the button, and watch for the request.
这篇关于通过jquery click事件在php中设置会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!