sjlsam Posted January 19, 2008 Share Posted January 19, 2008 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' Quote Link to comment https://forums.phpfreaks.com/topic/86804-checkboxes-and-sessions-nightmare/ Share on other sites More sharing options...
kopytko Posted January 28, 2008 Share Posted January 28, 2008 Have you instered session_start() at the beginning of your file? Quote Link to comment https://forums.phpfreaks.com/topic/86804-checkboxes-and-sessions-nightmare/#findComment-451648 Share on other sites More sharing options...
sjlsam Posted February 1, 2008 Author Share Posted February 1, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/86804-checkboxes-and-sessions-nightmare/#findComment-455627 Share on other sites More sharing options...
kopytko Posted February 2, 2008 Share Posted February 2, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/86804-checkboxes-and-sessions-nightmare/#findComment-456085 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.