Jump to content

[SOLVED] Losing it!


jeffrydell

Recommended Posts

Literally ... I'm 'losing' my arrays and I can't figgr out why.

 

One script (we'll call it script.php for now) does 3 things (with a session initiated each time you 'hit' the script):

 

First time in (get), I session_register() each array and set up all the needed array keys/values with " ", so they 'exist' because they'll be default values in an .html form.

 

include('form.html');

 

The form's action is 'script.php?action=show'

 

Second time in (show) it displays all the $_POST variables, adds the $_POST values to the appropriate Arrays, then displays an identical table using the arrays (so I can see that the values got transferred from $_POST to arrays properly.

 

At the bottom of that page, there's a link to call the script a THIRD time (script.php?action=edit) - allowing the user to 'edit' the choices they made in the form.  This is where we lose everything.  On the third time in to the script, I can't get any values from the arrays.

 

See it in action here:

 

http://cjrenterprises.biz/testform.php

 

Any ideas?  I've done this type of thing MANY times before and never had trouble 'losing' arrays or variables like this.

 

Thanks!

 

Jeff

 

 

Link to comment
Share on other sites

Here you go:

 

<?

session_start();

 

if (!isset($_SESSION['action']))

{

$_SESSION['action'] = "get";

 

}

else

{

if (!isset($_GET['action']))

{

$junk = "nothing";

}

else

{

$_SESSION['action'] = $_GET['action'];

}

}

 

switch ($_SESSION['action'])

{

case "get":

session_register("Entry");

session_register("Classes");

session_register("Divisions");

session_register("Levels");

session_register("Trial1");

session_register("Trial2");

session_register("Trial3");

session_register("Trial4");

$count = 1;

$Entry['category'] = " ";

WHILE ($count <= 10)

{

$Classes[$count] = " ";

$Divisions[$count] = " ";

$Levels[$count] = " ";

$Trial1[$count] = " ";

$Trial2[$count] = " ";

$Trial3[$count] = " ";

$Trial4[$count] = " ";

$count++;

}

echo "This is from the php script which is 'calling' the form. <br />";

echo "  <br />";

$_SESSION['action'] = "show";

include("NADAC_Generic.html");

break;;

case "show":

echo "<h3>Here are the results from the form:</h3>";

echo "<hr>";

echo "<b>Category:</b> " . $_POST['category'] . "<br />";

echo "<table><tbody>";

echo "<tr><td>" . $_POST['c1'] . ":</td><td>" . $_POST['d1'] . "</td><td>" . $_POST['l1'] . "</td><td>Entered: " . $_POST['t1c1'] . "</td><td>" . $_POST['t2c1'] . "</td><td>" . $_POST['t3c1'] . "</td><td>" . $_POST['t4c1'] . "</td></tr>";

echo "<tr><td>" . $_POST['c2'] . ":</td><td>" . $_POST['d2'] . "</td><td>" . $_POST['l2'] . "</td><td>Entered: " . $_POST['t1c2'] . "</td><td>" . $_POST['t2c2'] . "</td><td>" . $_POST['t3c2'] . "</td><td>" . $_POST['t4c2'] . "</td></tr>";

echo "<tr><td>" . $_POST['c3'] . ":</td><td>" . $_POST['d3'] . "</td><td>" . $_POST['l3'] . "</td><td>Entered: " . $_POST['t1c3'] . "</td><td>" . $_POST['t2c3'] . "</td><td>" . $_POST['t3c3'] . "</td><td>" . $_POST['t4c3'] . "</td></tr>";

echo "<tr><td>" . $_POST['c4'] . ":</td><td>" . $_POST['d4'] . "</td><td>" . $_POST['l4'] . "</td><td>Entered: " . $_POST['t1c4'] . "</td><td>" . $_POST['t2c4'] . "</td><td>" . $_POST['t3c4'] . "</td><td>" . $_POST['t4c4'] . "</td></tr>";

echo "<tr><td>" . $_POST['c5'] . ":</td><td>" . $_POST['d5'] . "</td><td>" . $_POST['l5'] . "</td><td>Entered: " . $_POST['t1c5'] . "</td><td>" . $_POST['t2c5'] . "</td><td>" . $_POST['t3c5'] . "</td><td>" . $_POST['t4c5'] . "</td></tr>";

echo "<tr><td>" . $_POST['c6'] . ":</td><td>" . $_POST['d6'] . "</td><td>" . $_POST['l6'] . "</td><td>Entered: " . $_POST['t1c6'] . "</td><td>" . $_POST['t2c6'] . "</td><td>" . $_POST['t3c6'] . "</td><td>" . $_POST['t4c6'] . "</td></tr>";

echo "<tr><td>" . $_POST['c7'] . ":</td><td>" . $_POST['d7'] . "</td><td>" . $_POST['l7'] . "</td><td>Entered: " . $_POST['t1c7'] . "</td><td>" . $_POST['t2c7'] . "</td><td>" . $_POST['t3c7'] . "</td><td>" . $_POST['t4c7'] . "</td></tr>";

echo "<tr><td>" . $_POST['c8'] . ":</td><td>" . $_POST['d8'] . "</td><td>" . $_POST['l8'] . "</td><td>Entered: " . $_POST['t1c8'] . "</td><td>" . $_POST['t2c8'] . "</td><td>" . $_POST['t3c8'] . "</td><td>" . $_POST['t4c8'] . "</td></tr>";

echo "<tr><td>" . $_POST['c9'] . ":</td><td>" . $_POST['d9'] . "</td><td>" . $_POST['l9'] . "</td><td>Entered: " . $_POST['t1c9'] . "</td><td>" . $_POST['t2c9'] . "</td><td>" . $_POST['t3c9'] . "</td><td>" . $_POST['t4c9'] . "</td></tr>";

echo "<tr><td>" . $_POST['c10'] . ":</td><td>" . $_POST['d10'] . "</td><td>" . $_POST['l10'] . "</td><td>Entered: " . $_POST['t1c10'] . "</td><td>" . $_POST['t2c10'] . "</td><td>" . $_POST['t3c10'] . "</td><td>" . $_POST['t4c10'] . "</td></tr>";

echo "</tbody></table>";

echo "<br />";

echo "<h3>Here are the values as they are stored:</h3>";

echo "<hr>";

// Loadin' up the Arrays...

$Entry['category'] = $_POST['category'];

$count = 1;

echo "<b>Category:</b> " . $Entry['category'];

echo "<table><tbody>";

WHILE ($count <= 10)

{

$ckey = "c" . $count;

$dkey = "d" . $count;

$lkey = "l" . $count;

$t1ky = "t1" . $ckey;

$t2ky = "t2" . $ckey;

$t3ky = "t3" . $ckey;

$t4ky = "t4" . $ckey;

$Classes[$count] = $_POST[$ckey];

$Divisions[$count] = $_POST[$dkey];

$Levels[$count] = $_POST[$lkey];

$Trial1[$count] = $_POST[$t1ky];

$Trial2[$count] = $_POST[$t2ky];

$Trial3[$count] = $_POST[$t3ky];

$Trial4[$count] = $_POST[$t4ky];

echo "<tr><td>" . $Classes[$count] . ":</td><td>" . $Divisions[$count] . "</td><td>" . $Levels[$count] . "</td><td>Entered: " . $Trial1[$count] . "</td><td>" . $Trial2[$count] . "</td><td>" . $Trial3[$count] . "</td><td>" . $Trial4[$count] . "</td></tr>";

$count++;

}

echo "</tbody></table>";

echo "<br />";

echo "<a href='testform.php?action=edit'>Click here to edit the entry</a>";

break;;

case "edit":

echo "<table><tbody>";

$count = 1;

WHILE ($count <= 10)

{

echo "<tr><td>" . $count . "</td><td>" . $Classes[$count] . ":</td><td>" . $Divisions[$count] . "</td><td>" . $Levels[$count] . "</td><td>Entered: " . $Trial1[$count] . "</td><td>" . $Trial2[$count] . "</td><td>" . $Trial3[$count] . "</td><td>" . $Trial4[$count] . "</td></tr>";

$count++;

}

echo "</tbody></table>";

include('NADAC_Generic.html');

break;;

}

 

?>

Link to comment
Share on other sites

Foser,

 

Thanks for the tip!

 

I'm not really setting the session with session_register(), I'm 'setting' it with the session_start(); at the top of the script (at least I think that's the way it works).

 

I'm using the session_register('ArrayName') to make those arrays available across scripts ... like the $_SESSION[] array.  This is a part of a much larger application that will walk people through an 'interview process' similar to online tax prep services, etc.  Each subject area has its own script, sometimes broken out depending on the organizations involved and different business rules requiring different data.

 

Please let me know if there's a better way to make those arrays available for as long as the visitor is in the session.

Link to comment
Share on other sites

Is register globals on or off?  Might have something to do with it, as you are not referring to the variables with the $_SESSION['variable'] name syntax but relying simply on $variable_name.  If register globals is off then these values would be empty.

 

http://www.phpriot.com/d/articles/php/fundamentals/intro-php-sessions/page7.html

 

Try this link for working with storing arrays in sessions.

Link to comment
Share on other sites

This script didn't work in my 'test' environment, but when I put it in the full project it worked fine ... so whatever it was isn't important right now.

 

Maybe someday I'll pay the price for not figuring it out now ... but for the time being, I'm moving on.  Thanks for your help anyway!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.