I want to switch queries so i can change my content on a specific div. Like if i choose Query A i get an specific group of content, QueryB other group of content. So i have used boolean $_SESSION to manipulate my condition on php file. Notice that $_SESSION is a 'superglobal', or automatic global, variable which means that it is available in all scopes throughout a script.
I have notice that click function doesn't switch session var. It seems this variable is read without clicking on anchor.
Here is my code:
html file:
<p><a class="queryA" href="">Query A</a> | <a class="queryB" href="">Query B</a></p>
</div>
Jquery function:
$('#SwitchTab .queryA').click(function(){
$('#chatbox').html(""); // turn chatbox empty
<?php $_SESSION['qQuery'] = TRUE;?>
return false;
});
$('#SwitchTab .queryB').click(function(){
<?php $_SESSION['qQuery'] = FALSE;?>
return false;
});
php:
if(isset($_SESSION['name'])){ //
$name = $_SESSION['name'];
$Userid= $_SESSION['idUser'];
}
if(!isset($_SESSION['qQuery'])){
$_SESSION['qQuery'] = FALSE;
}
$follow= $_SESSION['qQuery'];
include 'openconn.inc';
if($follow){ // Query A
$sql = "SELECT U.PhotoName, M.Mid, M.Author, M.MidUser, M.Message, M.MsgTime, M.TotLabelValue FROM messages AS M, users AS U, follow AS F WHERE U.Uid=M.MidUser AND F.idFrom=".$Userid." AND M.MidUser=F.idTo";
}
else{ //Query B
$sql = "SELECT U.PhotoName, M.Mid, M.Author, M.MidUser, M.Message, M.MsgTime, M.TotLabelValue FROM messages AS M, users AS U WHERE U.Uid=M.MidUser";
}
...
Question: Is there a way to switch a superglobal var with jquery?
thanks in advance
While you are only clicking, you are not reloading the page. You are doing that in client side. Thats why the session is not changing. In order to change the session you must send this information to the server.
Other issue in your code is, the $_SESSION['qQuery'] code you are running inside the javascript instruction always will run whne submitted to server. Both of them. So, when you load the page this variable will be false after pass the jquery instructions.
To change the value of this variable when click in the browser you can use ajax as recommended by @AbsoluteZERO. But, you need to change your code to something like:
$('#SwitchTab .queryA').click(function(){
$('#chatbox').html(""); // turn chatbox empty
ajaxInstructionToChangeSession();
return false;
});
$('#SwitchTab .queryB').click(function(){
ajaxInstructionToChangeSession();
return false;
});
It the server you must do something like:
<?php
if($parameterfromajax == 'queryA'){
$_SESSION['qQuery'] = TRUE;
}else{
$_SESSION['qQuery'] = false;
}
?>