/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Switch & Session

OK here’s the code

[code=php]

switch (true){

case ($_POST[‘cardType’]==’recognize’):

//$card_id=$_POST[‘card_id’];
$_SESSION[‘card1’]=$_POST[‘card_id’];
echo $_SESSION[‘card1’];
$table=display_cards(‘realize’);

break;

case ($_POST[‘cardType’]==’realize’):

//$card_id=$_POST[‘card_id’];
$_SESSION[‘card2’]=$_POST[‘card_id’];
echo $_SESSION[‘card2’];
$table=display_cards(‘reconnect’);

break;

case ($_POST[‘cardType’]==’reconnect’):

//$card_id=$_POST[‘card_id’];
$_SESSION[‘card3’]=$_POST[‘card_id’];
echo $_SESSION[‘card3’];
$table=display_cards(‘receive’);

break;

case ($_POST[‘cardType’]==’receive’):

$_SESSION[‘card4’]=$_POST[‘card_id’];
echo $_SESSION[‘card2’];
echo $_SESSION[‘card4’];

break;

default:

session_start();
$_SESSION[‘card1’]=”;
$_SESSION[‘card2’]=”;
$_SESSION[‘card3’]=”;
$_SESSION[‘card4’]=”;

$table=display_cards(‘recognize’);

break;
}

[/code]

It seems that this code resets the session variables every time I run it, so I end up only with the last one that was set.

I just can’t understand why, as the default shouldn’t run if one of the others is true

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NightcatauthorApr 15.2009 — OK

I tried using [code=php]elseif[/code] but it still wipes out all the variables except the last one set ?
Copy linkTweet thisAlerts:
@steve42lawsonApr 15.2009 — Perhaps your POST variables contain whitespace (assuming their content came from some sort of interface). Try:
[code=php]case (trim($_POST['cardType'])=='reconnect'):[/code]
or
[code=php]case (strstr($_POST['cardType'], 'reconnect') !== false):[/code]
or for case-insensitive [I]case[/I]
[code=php]case (stristr($_POST['cardType'], 'reconnect') !== false):[/code]

Also, try applying the following test to your POST data:
[code=php]isset($_POST['cardType'])[/code]

BTW: Have you [I]looked[/I] at the POST data to see if the strings you are testing for are really there?

[code=php]echo "$_POST['cardType']: ***{$_POST['cardType']}***";[/code]
[the stars ('***') are to make whitespace stand out.]

Also, are you sure you have the case right on your key: 'cardType'? Perhaps the following test will reveal something:
[code=php]var_dump($_POST);[/code]
Copy linkTweet thisAlerts:
@NightcatauthorApr 16.2009 — I've found a problem.

Basicaly, whenever I was writing my code, I would declare a session only in "default" part of the switch statement. This ment that whenever page loaded with any data POSTED to it, the session was not declared and the variables seem to not have been saved.

The solution seems to be writing session_start() in every instance of the "switch" statement.
Copy linkTweet thisAlerts:
@steve42lawsonApr 16.2009 — Doh!:o Didn't even notice that -- so what if you place the [FONT="Fixedsys"]session_start();[/FONT] call before the [FONT="Fixedsys"]switch[/FONT] statement [preferably at the top of the PHP block, before any other PHP code]? Does that eliminate the need for the multiple [FONT="Fixedsys"]session_start();[/FONT] calls within the [FONT="Fixedsys"]switch[/FONT] statement?
Copy linkTweet thisAlerts:
@NightcatauthorApr 16.2009 — There's been some problem with it, but I think it's down to the fact that I was declaring session variable on the top as well, so one would have to declare them in the "default" part of the switch statement
×

Success!

Help @Nightcat spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...