user4622110
user4622110

Reputation:

On click of button, check if user is logged in or not

I have a button that is responsible for performing multiple task, and has lot of scripting for it. When this button

<button type="submit" value="submit" name="submit" class="second_button">Negotiate</button>

the script applied on this button is

<script>

var startClock;
var submitamt;
var walkaway;
var digits;

$(function() {
  startClock = $('#startClock').on('click', onStart);
  submitamt = $('#submitamt').on('click', onSubmit);
  walkaway = $('#walkaway').on('click', onWalkAway);
  digits = $('#count span');
  beforeStart();
});

var onStart = function(e) {
  startClock.fadeOut(function() {
    startTimer();
    submitamt.fadeIn(function() {
      submitamt.trigger('click'); // fire click event on submit
    });
    walkaway.fadeIn();
  });
};

var onSubmit = function(e) {
  var txtbox = $('#txt').val();
  var hiddenTxt = $('#hidden').val();
  $.ajax({
    type: 'post',
    url: 'test2.php',
    dataType: 'json',
    data: {
      txt: txtbox,
      hidden: hiddenTxt
    },
    cache: false,
    success: function(returndata) {

         console.log(returndata[3]);
    // $('#proddisplay').html(returndata);
    },
    error: function() { 
      console.error('Failed to process ajax !');
    }
  });
};

var onWalkAway = function(e) {
  console.log('onWalkAway ...');
};

var counter;
var timer;
var startTimer = function() {
  counter = 120;
  timer = null;
  timer = setInterval(ticker, 1000);
};

var beforeStart = function() {
  digits.eq(0).text('2');
  digits.eq(2).text('0');
  digits.eq(3).text('0');
};

var ticker = function() {
  counter--;
  var t = (counter / 60) | 0; // it is round off
  digits.eq(0).text(t);
  t = ((counter % 60) / 10) | 0;
  digits.eq(2).text(t);
  t = (counter % 60) % 10;
  digits.eq(3).text(t);
  if (!counter) {
    clearInterval(timer);
    alert('Time out !');
    resetView();
  }
};

var resetView = function() {
  walkaway.fadeOut();
  submitamt.fadeOut(function() {
    beforeStart();
    startClock.fadeIn();
  });
};
</script>

before this script executes i wish to check if the user is logged in or not, In normal php i use this code

<?
if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true || isset($_SESSION['fb_logged_in']) && $_SESSION['fb_logged_in'] == true )
    {?>
        <!--display message-->
    <?}?>   

but i am not able to understand how to embed it with the script i wish to display a popup message if the user is not logged in and ask user to login first

Upvotes: 1

Views: 3195

Answers (2)

jaecktec
jaecktec

Reputation: 462

Embed the Session check in the http post. Return an error code (for example 400) when not logged in and process popup handling in the error function.

<?
  if (isset($_SESSION['loggedin']) 
  && $_SESSION['loggedin'] == true || isset($_SESSION['fb_logged_in']) 
  && $_SESSION['fb_logged_in'] == true ) {?> 
       <!-- display message -->
  <?}else{
       http_response_code(400);
    }?> 

Upvotes: 0

Sarath Kumar
Sarath Kumar

Reputation: 2353

it is not recommended to check it with client side scripting. try it with hidden variable..

<input type="hidden" name="userLog" id="userLog" value="<?=$_SESSION['fb_logged_in'];?>">

and in script check

if($('#userLog')!=''){  
  // do something..
}

Upvotes: 1

Related Questions