0

How to get the variable for count in test.php using ajax/jquery?

The variable that i want to get is that $countses. its responsible to hold a value of count for borrow session.

    //session_destroy();
if(isset($_SESSION['sBorrow'])){
    $countses = count($_SESSION['sBorrow']);

    echo $countses; 
}
    // nothing requested, so return all values
    print json_encode($_SESSION);

I want to call in this place

var sessionvar;
            $.ajaxSetup({cache: false})
            $.get('test.php' ,function (data) {
                sessionvar = data;
                //var ses = sessionvar.length
                //alert(ses);

                /*
                var count = 0;
                for(var i = 0; i < sessionvar.length; ++i){
                    if(sessionvar[i] == 2)
                        count++;
                    alert(count);
                }*/

                var checkedCbs = $('.sBorrow:checked ');
                if (checkedCbs.length === 2) {
                    alert("You can only select 3 books");
                    this.checked = false;       
                }
            });

I want to call the variable for $countses so that it can be used for conditional statement. However i only get the session variable. So i comment it for a bit while finding the solution.

5
  • only get the session variable? you don't get json_encode($_SESSION)? Commented Aug 6, 2016 at 10:33
  • i dont know about json_encode. And i still get the result. except i still having problem to use it. can you tell me where to use that json_encode Commented Aug 6, 2016 at 10:39
  • oh wait. didnt i already get it? i took the json_encode($_SESSION) right? Commented Aug 6, 2016 at 10:42
  • i don't know what you are trying to do.what is $_SESSION['sBorrow']? does it hold a array?you shouldn't use echo before json encode.it makes your json invalid.you may need to add count also to json object and return it Commented Aug 6, 2016 at 10:42
  • yes it holds an array Commented Aug 6, 2016 at 10:50

2 Answers 2

2

Edit the code in Test.php

$ret = [];
if(isset($_SESSION['sBorrow']) && is_array($_SESSION['sBorrow']))
{
    $countses = count($_SESSION['sBorrow']);
    array_push($ret,$countses); 
}

array_push($ret,$_SESSION);

print $ret;   
}

ADD this code in ajax

$.get('test.php' ,function (data)
{
var data = data[0];// this is your $countsess
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

test.php:

$ret = [];
if(isset($_SESSION['sBorrow']) && is_array($_SESSION['sBorrow']))
{
    $countses = count($_SESSION['sBorrow']);
    $ret['countses'] = $countses; 
}

$ret['session'] = $_SESSION;

print json_encode($ret);

Then your javascript:

var sessionvar;
var countsesvar;
.
.
$.get('test.php' ,function (data) {
    sessionvar = data.session;
    countsesvar = data.countses;
    .
    .
});

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.