Jump to content

php sessions with checkboxes


ashutoash

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.