Jump to content

checkboxes and sessions nightmare


sjlsam

Recommended Posts

i have a checkbox that i want to set a session variable based on whether it is selected or not, i have tried ALL the following, and for whatever reason it makes the session variable off for all of them (even if $_POST[$indie] == 'on'!!)

 

what am i doing wrong?

 

 

ATTEMPT 1:

if( $_POST[$indie] == 'on' ) { $_SESSION['indie'] = 'on'; } else { $_SESSION['indie'] = 'off'; }

 

RESULT:

$_SESSION['indie'] always returns as 'off' even when $_POST[$indie] returns as 'on'

 


 

ATTEMPT 2:

if( isset($_POST[$indie]) && $_POST[$indie] == 'on' ) { $_SESSION['indie'] = 'on'; } else { $_SESSION['indie'] = 'off'; }

 

RESULT:

$_SESSION['indie'] always returns as 'off' even when $_POST[$indie] returns as 'on'

 


 

ATTEMPT 3:

if( isset($_POST[$indie]) ) { $_SESSION['indie'] = 'on'; } else { $_SESSION['indie'] = 'off'; }

 

RESULT:

$_SESSION['indie'] always returns as 'off' even when $_POST[$indie] returns as 'on'

Link to comment
Share on other sites

  • 2 weeks later...

thanks for your reply, i have made some more adjustments to my code, but it seems that Corp is always on and can never be turned off

i think there is something wrong with my logic

 

what i am trying to accomplish here: i want indie and corp to be passed as a session, but if there is no session present the sessions need to be created based on the user's selection

 

i have been trying to figure out this problem on and off for a month!

 

 

here's my code:

 

PAGE 1

<?php

session_start();

session_register("SESindie");
session_register("SEScorp");

/* GRABBING THE VARIABLES FROM SESSION OR POST */

if (isset($_REQUEST["FRMindie"]) or isset($_REQUEST["FRMindie"])) {
if($_REQUEST["FRMindie"] != "on")	  { $indie = "off"; } else { $indie = "on"; }
if($_REQUEST["FRMcorp"] != "on")	   { $corp = "off"; } else { $corp = "on"; }

/* pass the selection to the session variables */
$_SESSION["SESindie"] = $indie;
$_SESSION["SEScorp"] = $corp;
}
else
{
$indie = $_SESSION["SESindie"];
$corp = $_SESSION["SEScorp"];
}

/* QUERY THAT RUNS  BASED ON YOUR SELECTIONS */
$query = "SELECT * FROM restaurants";
$isfirst = 1;

if($indie=='on'){
if($isfirst == 1){ $query .= " WHERE "; $isfirst = 0; } else { $query .= " AND "; }
$query .= "restaurants.restIndependent = 1";
}

if($corp=='on'){
if($isfirst == 1){ $query .= " WHERE "; $isfirst = 0; } else { if($indie == 'on') { $query .= " OR "; } else { $query .= " AND "; } }
$query .= "restaurants.restIndependent = 0";
}

/* CODE FOR CHECKBOXES */
echo "<form name='search' action='./sessions.php' method='post'>";

/* INDIE CHECKBOX */	
echo "Indie <input name='FRMindie' type='checkbox' onchange='this.form.submit()'";
if ($indie == "on"){ echo " checked='checked' "; }
echo "/>  -  ";

/* CORP CHECKBOX */
echo "Corp <input name='FRMcorp' type='checkbox' onchange='this.form.submit()'";
if ($corp == "on"){ echo " checked='checked' "; }
echo "/>";

echo "</form>";

/* DEBUG */
echo "<div style='background:lightgrey; width:400px;'><strong>DEBUG</strong>";

echo "<br /><br />QUERY: $query<br /><br />";

echo "SESSION indie: " . $_SESSION["SESindie"] . "<br />";
echo "SESSION corp: " . $_SESSION["SEScorp"] . "<br /><br />";

echo "FORM indie: " . $_REQUEST["FRMindie"] . "<br />";
echo "FORM corp: " . $_REQUEST["FRMcorp"] . "</div><br /><br />";

echo "<a href='sessions_forward.php'>Change page</a>";

?>

 

PAGE 2

<?php session_start(); ?>

You went forward!<br /><br />

<?php echo "Indie: " . $_SESSION["SESindie"]; ?>

<br />

<?php echo "Corp: " . $_SESSION["SEScorp"]; ?>

<br /><br />
<a href="sessions.php">Back</a>

Link to comment
Share on other sites

Try this:

<?php
/* INDIE CHECKBOX */	
echo "Indie <input name='FRMindie' type='checkbox' value='on' onchange='this.form.submit()'";
if ($indie == "on"){ echo " checked='checked' "; }
echo "/>  -  ";

/* CORP CHECKBOX */
echo "Corp <input name='FRMcorp' type='checkbox'value='on'  onchange='this.form.submit()'";
if ($corp == "on"){ echo " checked='checked' "; }
echo "/>";
?>

 

And another thing. $_REQUEST does not consist $_SESSION variables. It consisting $_GET, $_POST and $_COOKIE variables.

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.