Forum Moderators: coopster

Message Too Old, No Replies

$_SESSION array not taking it's values

         

Sarah Atkinson

4:49 pm on Jun 23, 2005 (gmt 0)

10+ Year Member




?php
session_start();
if(!isset($_SESSION)){
$_SESSION=array();
}
if (isset($_POST['scheck'])) {
foreach ($_POST as $key => $val) {
if(ereg('^a[0-9]+$',$key)){
$_SESSION['$key'] = $val;
}}
}
elseif (isset($_POST['from1check'])) {
$form1var="fname lname email month day year gender street city state zip phone ename ephone pname pphone iname iphone med allergies form1check";
$form1vararray=explode(" ",$form1var);
foreach($form1vararray as $key=> $val){
$_SESSION['$val']=$_POST['$val'];
}}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

here is the session data but everytime I check the session values the array allways stays empty.

Sarah

sned

4:57 pm on Jun 23, 2005 (gmt 0)

10+ Year Member



$_SESSION['$val']=$_POST['$val'];

I believe the '$val' is being interepreted as a literal $val, so the session variable $val is being set to $val :) .. if that makes any sense.

Try: $_SESSION[$val] = $_POST[$val]

hope that helps ..

-sned

<edit>
oops .. removed my own '' :)
</edit>

Sarah Atkinson

5:02 pm on Jun 23, 2005 (gmt 0)

10+ Year Member



nope that's not the problem. when doing a printing of the session array it give simply array()

Sathallrin

5:23 pm on Jun 23, 2005 (gmt 0)

10+ Year Member



How are you printing out $_SESSION?

StupidScript

5:56 pm on Jun 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Sarah,

$_SESSION
is always an array, anyway. You don't need to establish that. Your first test (
if (!isset($_SESSION)){
) is not needed as you have it.
session_start()
guarantees read/write access to the
$_SESSION
elements.

If you want to see if there is anything in there, test for something you have already written to the session, like

if (!isset($_SESSION['fname'])){
.

Next, you do, indeed need to reference the variable as a variable, and not as a literal string. Use

$_SESSION[$val]
instead of
$_SESSION['$val']
if you want
fname
and the rest. (Of course, if you want only one element (
$val=>whatever
), then you're doing the right thing ... but I suspect you want
fname
instead. ;)

Last, as you loop through your array toward the end (

foreach
), you need to fix something.

If your

$_POST
array looks like:

('Betty','Grable','me@email.com','June' ... etc. )

Then you need to use:

foreach($form1vararray as $key => $val){

$_SESSION[$val]=$_POST[$key];

}

If your

$_POST
array looks like:

(fname => 'Betty',lname => 'Grable',email => 'me@email.com',month => 'June' ... etc. )

Then you can use:

foreach($form1vararray as $val){

$_SESSION[$val]=$_POST[$val];

}

In either case, you end up with

$_SESSION
as
fname => Betty, lname => Grable, etc.

If you want to check what data are in the arrays, try:

print_r($_POST);
then,
print_r($_SESSION);

Sarah Atkinson

6:08 pm on Jun 23, 2005 (gmt 0)

10+ Year Member




foreach($form1vararray as $key => $val){
$_SESSION[$val]=$_POST[$key];
}

If your $_POST array looks like:

(fname => 'Betty',lname => 'Grable',email => 'me@email.com',month => 'June' ... etc. )

Then you can use:

foreach($form1vararray as $val){
$_SESSION[$val]=$_POST[$val];
}

In either case, you end up with $_SESSION as fname => Betty, lname => Grable, etc.

this is what my post array looks like.

(fname => 'Betty',lname => 'Grable',email => 'me@email.com',month => 'June' ... etc. )


Actually, I would suggest:

foreach($_POST as $val){
$_SESSION[$val]=$val;
}

That would include everything in the $_POST array without going through empty values specified in your $form1vararray array that might not be posted.


I think that is what I don't want to do.
I only want those varriables to be added to the session and nothing that may be tagging along for the ride.

If you want to check what data are in the arrays, try:

print_r($_POST); then,
print_r($_SESSION);

i'm printing it out with this

print_r($_SESSION);

It is still not puting anythign into the array
but this one does work
$_SESSION['checksession'] = 1;

Sarah Atkinson

6:24 pm on Jun 23, 2005 (gmt 0)

10+ Year Member



I got this to work if I take out the if statments. not sure how good of a practice this is... but at least it seems to work.

StupidScript

6:33 pm on Jun 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A quick clarifier, hopefully ...

(And sorry about the "Actually ..." comment I made earlier. I took it out after further thought, but you got to it first! :)

What does

foreach
mean? "
each
" what?

It means "each key" in the array.

('betty','grable' ...)

Each key in this case is

0,1, ...

(fname => 'betty',lname => 'grable' ...)

Each key in this case is

fname,lname, ...

So you are using your

$form1vararray
values to assign key names to your
$_SESSION
array elements.

#set variable

$val
to each key's value in
$form1vararray
(0,1,...)
foreach($form1vararray as $val){

#set the

$_SESSION
key using the variable
$_SESSION[$val]

#with its value equal to the same key's value from the

$_POST
array
= $_POST[$val];

If the

$_POST
array doesn't contain a key from
$form1vararray
, it's included but empty in the
$_SESSION
array.

(By the way, you could skip the

explode
step and just set
$form1vararray = array('fname','lname',...)
)

Y'know ... I wonder about the code you initially posted. If that was copy-and-pasted, then your problem is much more simple.

In your

elseif
statement, you ask if "from1check" is set. You probably want "form1check", instead.

There's nothing wrong with your code except for that typo, so either the typo is the problem or your

$_POST
array isn't using the same keys as are included in
$form1vararray
.

Sarah Atkinson

4:45 am on Jun 24, 2005 (gmt 0)

10+ Year Member



(By the way, you could skip the explode step and just set $form1vararray = array('fname','lname',...))

This is pure lazyness on my part. you see I had a text list of all the names for the fields for my form. I was too lazy to insert all the , and ' ' so I let a PHP function do it for me.

I thought aboult doing a rube goldberg and writing some code that would output the list complete with '' and , but figured it was too much work. (not that I haven't done stuff like that before)


Y'know ... I wonder about the code you initially posted. If that was copy-and-pasted, then your problem is much more simple.

In your elseif statement, you ask if "from1check" is set. You probably want "form1check", instead.

actualy that typo is in the form... didn't notice it till you pointed it out. I c/p it into the Session area so at lease i was consistant

Sarah