0

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.

12
  • and try to unset your $_SESSION variable before set it again Commented Apr 29, 2013 at 22:52
  • i think that's where my problem is. the variables aren't received in php. if I would try to just do $_SESSION[ 'display' ] = $_POST[ 'setdisplay' ]; nothing is changing. but setting the default like above is working Commented Apr 29, 2013 at 22:54
  • try to print_r($_POST) in your php to see what your post array has Commented Apr 29, 2013 at 22:56
  • it prints an empty array. so that's clear :) i tried passing the variables through an $.ajax call, but same result Commented Apr 29, 2013 at 22:58
  • and if you don't receive any variable check your path on jQuery, remember if the php file is on a folder outside you jQuery execution file you need to add ../ at the beginning of your php file path at jQuery $.post() definition Commented Apr 29, 2013 at 22:58

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

the call to fx.behaviour.php is 200 OK with the following url query: branded89.nl/projects/mvc/library/… the variables are in there "setdisplay=gridview"
your method worked! I had to give in on making it into a function, and just call the ajax-call in the click event. Thanks a lot!
0

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

UPDATE

Try this

1 Comment

tried putting the jsonp php parse code in my php code, and added the headers, but no results so far :(
0

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 );
   }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.