Welcome to Dream.In.Code
Become a PHP Expert!

Join 137,424 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,949 people online right now. Registration is fast and FREE... Join Now!




checkboxes and sessions nightmare

 
Reply to this topicStart new topic

checkboxes and sessions nightmare

sjlsam
23 Jan, 2008 - 08:27 PM
Post #1

New D.I.C Head
*

Joined: 23 Jan, 2008
Posts: 7

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:
CODE
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:
CODE
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:
CODE
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'
User is offlineProfile CardPM
+Quote Post

dontKnowJava
RE: Checkboxes And Sessions Nightmare
24 Jan, 2008 - 11:37 AM
Post #2

D.I.C Head
**

Joined: 29 Sep, 2007
Posts: 213


My Contributions
well checkboxes dont return on or off. theyre either checked or not. so something like this should work assuming your checkbox name = 'indie'
CODE

if(!empty($_POST['indie']))

User is offlineProfile CardPM
+Quote Post

sjlsam
RE: Checkboxes And Sessions Nightmare
24 Jan, 2008 - 08:57 PM
Post #3

New D.I.C Head
*

Joined: 23 Jan, 2008
Posts: 7

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
CODE
<?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
CODE

<?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>

User is offlineProfile CardPM
+Quote Post

sjlsam
RE: Checkboxes And Sessions Nightmare
1 Feb, 2008 - 01:26 PM
Post #4

New D.I.C Head
*

Joined: 23 Jan, 2008
Posts: 7

i know its been awhile, but i've still got the same problem
can someone look at my code above and see what's wrong?
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Checkboxes And Sessions Nightmare
1 Feb, 2008 - 03:55 PM
Post #5

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,260



Thanked: 227 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
The problem appears to be with the following code...

CODE


if (isset($_REQUEST["FRMindie"]) or isset($_REQUEST["FRMCorp"])) {

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


First off I think you meant to make the second check in your or statement to be FRMCorp and not FRMindie again. You were testing the same item twice.

Second, I didn't have problems with Corp, I had problems with indie checkbox. You were right about it being a logic problem. When you select the indie checkbox it makes $indie on and stores it in the session... great. Good so far. The problem is when you turn it off with the checkbox. When you check it off, the first if statement above checks to see if FRMindie is set. It isn't (because you checked it off) and so it loads up the "on" from the session and checks the box again. You go to check it off again and the cycle goes right back through, it is off again, so it loads from session and turns it on again. So the indie checkbox never turns off.

You could fix this by simply testing for the existence of FRMindie to begin with and in turn set $indie on or off based on that alone. Make a separate test for FRMCorp.

CODE

if (isset($_REQUEST["FRMindie"])) {
     $_SESSION["SESindie"] = "on";
}
else { $_SESSION["SESindie"] = "off"; }

if (isset($_REQUEST["FRMcorp"])) {
     $_SESSION["SEScorp"] = "on";
}
else { $_SESSION["SEScorp"] = "off"; }

$indie = $_SESSION["SESindie"];
$corp = $_SESSION["SEScorp"];


Here we detect whether or not the checkbox was even set and if it was, set the session var to on. Otherwise turn it off in the session. Same with corp. At the end we take whatever the status is in the session and populate the $indie and $corp variables.

Lastly, you might want to use "||" instead of "or" in the future for any joining of conditions.

This should fix your problems. Enjoy! smile.gif

"At DIC we be session manipulating code ninjas!" decap.gif


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/5/08 04:36AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month