Reputation: 15
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'
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.
Here's my code:
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 });
}
}
and my PHP in fx.behaviour.php
$_SESSION[ 'display' ] = 'listview'; // set default
if ( $_POST[ 'setdisplay' ] != '' ) {
$view = $_POST[ 'setdisplay' ];
$_SESSION[ 'display' ] = $view;
}
In short: the sessionvariable won't update after clicking the url and passing the value through jquery to php.
Upvotes: 0
Views: 14533
Reputation: 13080
Here's what I would do (cut some code out for quickness):
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';
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.
Upvotes: 2
Reputation: 5520
I don't think you should have {} when accessing string within scope of function:
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 );
}
}
Upvotes: 0
Reputation: 494
try to include your php into the jQuery file (just to test) and echo something echo 'Foobar';
and in your jQuery execution file add <?php include('../library/fx.behaviour.php');
and try changing the directory in the include line
Upvotes: 0