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
https://forums.phpfreaks.com/topic/238895-php-sessions-with-checkboxes/
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";

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.