ashutoash Posted June 9, 2011 Share Posted June 9, 2011 Hi, I am trying to use PHP session variables with checkboxes. My page has 3 checkboxes and those display and hide data according to check and uncheck. This page automatically refreshes every 5 mins. I want to save the checkboxes session and let them be the same on refresh or reload. I am passing the values of the checkboxes that is (on or off) via ajax to another page so that I can get them back on the main page. No matter whatever I do, the boxes are always on. Help lease. Main page code: session_start(); /* Includes */ date_default_timezone_set("UTC"); include("include/SPDB.php"); //include("include/database.php"); if(!isset($_SESSION["PL"])) { $_SESSION["PL"] = $_GET["pl"]; } if(!isset($_SESSION["JIT"])) { $_SESSION["JIT"] = $_GET["jit"]; } if(!isset($_SESSION["LAT"])) { $_SESSION["LAT"] = $_GET["lat"]; } Div tag on this page: <div id="metricoptions"> <? if($_SESSION["PL"] == "on") echo "<input id=\"ploss\" type=\"checkbox\" name=\"metricoptions\" checked=\"checked\" />Packet Loss"; else echo "<input id=\"ploss\" type=\"checkbox\" name=\"metricoptions\" checked=\"checked\"/>Packet Loss"; if($_SESSION["JIT"] == "on") echo "<input id=\"jitter\" type=\"checkbox\" name=\"metricoptions\" checked=\"checked\" />Jitter"; else echo "<input id=\"jitter\" type=\"checkbox\" name=\"metricoptions\"/>Jitter"; if($_SESSION["LAT"] == "on") echo "<input id=\"latency\" type=\"checkbox\" name=\"metricoptions\" checked=\"checked\"/>Latency"; else echo "<input id=\"latency\" type=\"checkbox\" name=\"metricoptions\"/>Latency"; ?> </div> Ajax call on this page: $.ajax({ type: "GET", url: 'sessionVars.php?pl=' + $('#ploss').val() + '&jit=' + $('#jitter').val() + '&lat=' + $('#latency').val(), }); sessionVars page: session_start(); $_SESSION["PL"] = $_GET["pl"]; $_SESSION["JIT"] = $_GET["jit"]; $_SESSION["LAT"] = $_GET["lat"]; Also I use live click function for the checkboxes. Thanks. MOD EDIT: code tags added. Quote Link to comment Share on other sites More sharing options...
fugix Posted June 9, 2011 Share Posted June 9, 2011 firstly, you have not assigned unique values to your check boxes, so I'm curious as to how you are checking to see which ones are checked before refresh..?Also, have you checked to see what is getting passed to your sessionvars.php page Quote Link to comment Share on other sites More sharing options...
ashutoash Posted June 9, 2011 Author Share Posted June 9, 2011 @Fugix: When I alert the checkboxes, the value "on" is displayed and this is getting passed to the sessionVars page and the same thing is coming back to the main page. Quote Link to comment Share on other sites More sharing options...
fugix Posted June 9, 2011 Share Posted June 9, 2011 if($_SESSION["PL"] == "on") echo "<input id=\"ploss\" type=\"checkbox\" name=\"metricoptions\" checked=\"checked\" />Packet Loss"; else echo "<input id=\"ploss\" type=\"checkbox\" name=\"metricoptions\" checked=\"checked\"/>Packet Loss"; here you are saying that regardless if the person has checked it or not, it will echo a checked box. do you mean if($_SESSION["PL"] == "on") echo "<input id=\"ploss\" type=\"checkbox\" name=\"metricoptions\" checked=\"checked\" />Packet Loss"; else echo "<input id=\"ploss\" type=\"checkbox\" name=\"metricoptions\" />Packet Loss"; Quote Link to comment Share on other sites More sharing options...
ashutoash Posted June 9, 2011 Author Share Posted June 9, 2011 Yeah by default Packet Loss will be checked always and others vary according to check uncheck. Do you suggest that I give each of the checkboxes a unique value like 1,2,3 or something and check if those values are posted via Ajax? Quote Link to comment Share on other sites More sharing options...
fugix Posted June 9, 2011 Share Posted June 9, 2011 in a nutshell, yes give each checkbox a unique value Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted June 9, 2011 Share Posted June 9, 2011 When posting code, enclose it within the forum's . . . BBCode tags. Quote Link to comment Share on other sites More sharing options...
ashutoash Posted June 9, 2011 Author Share Posted June 9, 2011 I have given them unique values....but is there some thing wrong with my ajax part? Say if I am on a page now and have ploss checked, jitter checked and latency unchecked....the ajax call no matter what is only sending one value even if it's set otherwise and on refresh all the three are returned checked. Are there any ajax function like on refresh or on reload? $.ajax({ type: "GET", url: 'sessionVars.php?pl=' + $('#ploss').val() + '&jit=' + $('#jitter').val() + '&lat=' + $('#latency').val(), }); Quote Link to comment Share on other sites More sharing options...
fugix Posted June 9, 2011 Share Posted June 9, 2011 honestly i don't understand what you are saying in the first part of your explanation, my apologies, second.. did you use the if else statement that i posted for the remainder of your check boxes? if you did, there is no way that the default is to have them checked. Quote Link to comment Share on other sites More sharing options...
ashutoash Posted June 9, 2011 Author Share Posted June 9, 2011 Never mind ...I figured it out. Thanks for you help. Quote Link to comment Share on other sites More sharing options...
fugix Posted June 9, 2011 Share Posted June 9, 2011 Never mind ...I figured it out. Thanks for you help. no problem, can you post what your solution was? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.