Forum Moderators: coopster

Message Too Old, No Replies

Session Array Value Problem - Take 2

         

neophyte

11:32 am on Aug 31, 2007 (gmt 0)

10+ Year Member



Different day, same problem, begging for insight. I was about to give up on this but it's just bugging me to death and I just can't let it go... a personality dysfunction of mine, no doubt. My previous example was a bit complex so I've cut my code way down - still with no luck solving the issue - so maybe it's easier to understand what I'm doing.

And, by the way, I HUGELY appreciate everyone's patience with me!

Here goes:

On a form page, I've set up a multi-dimensional array - which resides in a session - which will hold various information about any number of form fields.

For my example here, there's a 'Personal' $_Session which holds arrays for a first name and last name form field like so:

$_SESSION['Personal'] = array
(
'first' => array
(
'name' => 'first',
'type' => 'text',
'value' => NULL,
'validate' => TRUE,
'empty' => FALSE,
'invalid' => FALSE,
'hcode' => FALSE,
'function' => 'firstname()'
),
'last' => array
(
'name' => 'last',
'type' => 'text',
'value' => NULL,
'validate' => TRUE,
'empty' => FALSE,
'invalid' => FALSE,
'hcode' => FALSE,
'function' => 'lastname()'
)
);

There's also a session which holds an array of set names as well:

$_SESSION['TSets'] = array
(
'Personal'
);

There are separate sessions for set names and set contents because I'm trying not to over complicate my rather simple mind. At any rate, both of the above works fine.

Now, when the form get's submitted, a validate routine is brought into play. This routine checks to see if anything's been posted for each of the members of the array; if POST is not empty for any given field, it preforms further validation based upon what is suppose to be in each field. This works as well (routine shown below).

//Loop through each set array
foreach($_SESSION['TSets'] as $key => $value)
{
//Transfer the name of each set into $set
$set = $_SESSION['TSets'][$key];

//Loop through each field array
foreach($_SESSION[$set] as $field => $value)
{
//Get the name of the current field - WORKS IN THE VALIDATION ROUTINE
$name = $_SESSION[$set][$field]['name'];

//Get type of the current field - WORKS IN THE VALIDATION ROUTINE
$type = $_SESSION[$set][$field]['type'];

//Get the $_POST value and SET the $_SESSION['value'] value with the post value
//Also transfer the $_POST value into $postValue
//- WORKS IN THE VALIDTATION ROUTINE
$postValue = $_SESSION[$set][$field]['value'] = $_POST[$name];

// DO VALIDATION STUFF

...
}
}

After the validation is completed, if there are errors (a required field is left empty; the wrong type of data is in the wrong type of field, etc) the original form re-displays and empty fields show up red, invalid data fields show up yellow, fields that ARE valid re-display their pre-post contents, and so on.

This is where I'm having my problem: for some reason, my post values are not "getting into" the session arrays. What's REALLY weird is that I can successfully echo the posted session array values from the validation page, but when the form page re-displays, the original sessions remain unchanged from their initial values.

What's going on? Is the validation routine somehow NOT putting the Posted values into the correct "sessioned" multi-dimensional array? All that I can think of at this point is that somehow my addressing of the arrays with the "Personal" session is somehow mucked up, but I can't track down where I'm going wrong because if I echo out $set it is "Personal"; if I echo out $field, it is "First" or "Last"; if I echo out $type, it is "Text". And $postValue works as well. AND if I echo $_SESSION[$set][$field]['value'] after the $_Post value has been input, the value comes out as expected. But this makes me wonder if $set,$field,'value' is actually being fed into the correct session...

If you've got:

$_SESSION['Personal'] = array
(
'first' => array
(
'name' => 'first',
'type' => 'text',
'value' => NULL,
...

and the $_Post value is "Sam" and $set is "Personal" and $field is"first" and you want the $_Post value to go into personal>first>value, then isn't "Sam" going into $_Session['Personal']['first']['value']?

I've tried $_Session['TSets']['Personal']['first']['value'] but that doesn't seem correct and doesn't work either.

Are my nested loops somehow over-writing each other?

I have done a session_start() at the top of the index page, so I know it's not that.

Ladies and Gents, this question has grown into a tome and I hugely apologize; when I first started down this road, I thought this would be a simple task: famous last words!

If there's something obvious that I'm doing wrong (looping problem/ initial array set up/ addressing the session arrays improperly) I sure would appreciate an indication of where I'm going wrong.

Thanks to all.

Neophyte

d40sithui

11:52 am on Aug 31, 2007 (gmt 0)

10+ Year Member



Could this be the culprit?
$postValue = $_SESSION[$set][$field]['value'] = $_POST[$name];

shouldn't it be the opposite?
$_SESSION[$set][$field]['value'] = $_POST[$name];

neophyte

1:41 pm on Aug 31, 2007 (gmt 0)

10+ Year Member



d40sithui-

Thanks for your response; No, I don't believe so as this line is simply (or is suppose to be) setting the value of the array in the session var AS WELL AS initializing a local var (for validation routine purposes).

As a test after your post, I've tried your suggestion and have also removed all validation routines which would... at a minimum... simply make the the form page bounce back and echo the Posted input back into my "first name" form field:

<td class="w75"><label for="first">First Name:</label></td>
<td class="w175">
<input
type="text"
name="first"
value="<?php echo $_SESSION['Personal']['first']['value']?>"
id="first"
/>
</td>

The form bounced back, but the session remained unaltered (i.e. no post value was entered into the array in the session var) from it's original un-validated state.

Which is proved when I echo $_SESSION['Personal']['first']['value'] on my form page which continues to yield a NULL value regardless of what I input/submit in that form field.

Darn! That would have been such a simple fix.

Any other suggestions? Please?

d40sithui

3:07 pm on Aug 31, 2007 (gmt 0)

10+ Year Member



can you post your entire script, including your form and the part that processes your form.

neophyte

1:43 am on Sep 1, 2007 (gmt 0)

10+ Year Member



d40sithui, jatar k, eelixduppy -

With not a small amount of embarrassment I can now report that I have found my error.

I created a condition at the beginning of the form page which checked if a variable ($chkValidated) was set. If $chkValidated WASN'T set then the arrays would load with their initial values - this would be correct for the "first run" of the page.

When the validation routine came into play - after the form page was submitted - $chkValidated would then be set.

The, ah, problem was, that in the validation routine, this var was mis-spelled $chkvalidated (no capital "V"). So, of course, the original form page never saw that this var was checked and always over-wrote any sessions that were set by the validation routine.

Have corrected this minor (yet massive) oversight and everything now works as planned.

OMG, I feel like a complete idiot and heartily apologize for my two gusty posts on "why won't this work?". I should have caught this DAYS ago. Thanks to you three (and anyone else who waded through my two posts) for your time and assistance - next time I'll be combing all details of a complex script before posting.

Again, with apologies and great and humble thanks,

Neophyte

jatar_k

3:21 am on Sep 1, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



no need to apologize, I've done worse

more than a few times

glad you found it