Jump to content

Save Values in New Form? Scope?


gmark

Recommended Posts

Well, folks, thanks to all the help, I've narrowed my problem

down to needing a way to allow a variable value to traverse

into subsequent calls of a function.

 

I have a simple integer - "companyID", and it's set in "actionfile.php"

which is called by two forms.  The first time it's called by Form A,

it's set.  I'd LIKE to put it into $_SESSION, but apparently $_SESSION

is zeroed out before I can use it in the second call to "actionfile.php"

by Form B.  A new form/function call is a new session?

 

I've tried setting it to global and static, to no avail.

 

Anything more elegant than writing the value to SQL for later

retrieval?

 

Thanks!

 

Mark

Link to comment
Share on other sites

No -- it seems that I'm retaining control the whole time and not calling

start_session at all.  The flow is:

 

MAIN_FUNCTION (uses $_SESSION variable successfully, sets

$_SESSION['companyID'] properly, never writes to it again)

 

Calls FORM1 - which calls the MAIN_FUNCTION again

Inside MAIN_FUNCTION, $_SESSION['companyID'] no longer

prints out.  And NEITHER does the $my_company variable that

was set to the same value. 

 

And I've tried to set $my_company to a global and to static variables,

but the company ID value never makes it to the next call of the MAIN_FUNCTION.

 

Mark

Link to comment
Share on other sites

The following works swimmingly if I hardcode the "company ID" value at the top

of the file.  Each time this function is called, the "company ID" value -- whether stored

in a static or global or local variable, such as $my_companyID or $_SESSION['companyID'],

no longer prints out.

 

<?php

 

set_time_limit(0);

require_once 'C:\mpower\slipstream\trunk\www\administrator\uploader-inserter\Xsettings.php';

require_once 'C:\mpower\slipstream\trunk\www\administrator\uploader-inserter\class\util.php';

require_once 'C:\mpower\slipstream\trunk\www\administrator\uploader-inserter\class\msdb.php';

require_once 'C:\mpower\slipstream\trunk\www\administrator\uploader-inserter\class\import_exception.php';

require_once 'C:\mpower\slipstream\trunk\www\administrator\uploader-inserter\class\table_loader.php';

 

 

$connfail = false;

$table1_name = 'company';

$table2_name = 'ContactMap';

 

$my_companyid = $_SESSION['company_id'];

 

# display variables

$display_form = false;

$headers = array();

$errors = array();

 

if(isset($_POST['unique'])){

$unique_nm = $_POST['unique'];

}else{

$unique_nm = microtime(false);

$unique_nm = str_replace(" ", "", $unique_nm);

$unique_nm = str_replace(".", "", $unique_nm);

}

 

# path to save file as

$dest = dirname(__FILE__) . '\\'. FILE_UPLOAD_DIR . '\\' . $unique_nm;

 

## load spreadsheet into database table via form translation

if(isset($_POST['savetotable'])){

$db = new table_loader();

print_r($my_companyid);

print_r($_SESSION['company_id']);

 

$columns1 = $db->get_table_columns($table1_name);

$columns2 = $db->get_target_columns($my_companyid);

 

# $columns = array('id', 'name', 'quantity', 'other');

# gather data from csv file

$data = util::parse_csv_file_data($dest);

$translate1 = array();

$translate2 = array();

 

# gather translation from form

foreach($columns1 as $table1_column){

if(isset($_POST[$table1_column]) && $_POST[$table1_column] != ''){

$translate1[$_POST[$table1_column]] = $table1_column;

}

}

 

foreach($columns2 as $table2_column){

if(isset($_POST[$table2_column]) && $_POST[$table2_column] != ''){

$translate2[$_POST[$table2_column]] = $table2_column;

}

}

 

# save data to database

 

$total_inserted1 = $db->save_datasets($table1_name, $data, $translate1);

 

$total_inserted2 = $db->save_datasets($table2_name, $data, $translate2);

 

echo 'You have just uploaded ' . $total_inserted1 . ' fully-screened, processed and verified records.';

exit;

 

}elseif(isset($_POST['tableupload'])){

 

## setup to display form to translate spreadsheet to database table

# validate and upload file

$errors = util::upload_file('table', $dest, array("text/csv",

"text/comma-separated-values",

"application/csv",

"application/excel",

"application/vnd.ms-excel",

"application/vnd.msexcel",

"text/anytext"));

 

if(!empty($errors)){

$display_form = true;

}else{

# gather csv headers

$headers = util::parse_csv_file_headers($dest);

 

$db = new table_loader();

# columns1 are table column names ("company")

$columns1 = $db->get_table_columns($table1_name);

 

# columns2 are actual final target table columns names in

# "Contact" (derived from "ContactMap"), and labels will be derived from those

$columns2 = $db->get_target_columns($table2_name);

 

# $columns = array('id', 'name', 'quantity', 'other');

 

######################################################

###    Second Page Form

######################################################

?>

<form action="http://localhost/jetstream/administrator/uploader-inserter/Xpage_data_import.php" method="post">

<input type="hidden" id="unique" name="unique" value="<?php echo $unique_nm; ?>" />

<input type="hidden" id="savetotable" name="savetotable" value="1" />

<?php foreach($columns1 as $c_name){ ?>

<div>

<div style="float: left; width: 300px;"; >

<?php echo $c_name; ?>

</div>

<div style="float: left; width: 400px;">

<select id="<?php echo $c_name; ?>" name="<?php echo $c_name; ?>">

<option value=""></option>

<?php foreach($headers as $h_name){ ?>

<option value="<?php echo trim($h_name); ?>">

<?php echo trim($h_name); ?>

<?php print_r($_SESSION); ?>

</option>

<?php } ?>

</select>

</div>

<div style="clear: both;"></div>

</div>

<?php } ?>

 

<?php foreach($columns2 as $c_name){ ?>

<div>

<div style="float: left; width: 300px;"; >

<?php echo $c_name; ?>

</div>

<div style="float: left; width: 400px;">

<select id="<?php echo $c_name; ?>" name="<?php echo $c_name; ?>">

<option value=""></option>

<?php foreach($headers as $h_name){ ?>

<option value="<?php echo trim($h_name); ?>">

<?php echo trim($h_name); ?>

</option>

<?php } ?>

</select>

</div>

<div style="clear: both;"></div>

</div>

<?php } ?>

 

<input type="submit" value="Save <?php echo trim($table1_name); ?> and <?php echo trim($table2_name); ?> to Database?" />

</form>

 

<?php

 

}

 

}else{

$display_form = true;

}

 

######################################################

###    First Page Form

######################################################

 

if($display_form){

if(!empty($errors)){

foreach($errors as $an_error){

?>

 

<ul>

<li><?php echo $an_error; ?></li>

</ul>

<?php }

}

?>

<div class="module_container" id="divContactList">

<p>

 

<form action="http://localhost/jetstream/administrator/uploader-inserter/Xpage_data_import.php" method="post" enctype="multipart/form-data">

<input type="hidden" id="tableupload" name="tableupload" value="1" />

<input type="file" id="table" name="table" />

<input type="submit" id="enter" value="Upload CSV File with ONE Row of Labels, NO Empty Columns:" />

</form>

<?php                                                                                                   

}

?>

 

Link to comment
Share on other sites

I've circumvented this problem temporarily by placing the "companyID" value inside

$_POST['companyID'], which transfers fine.

 

Either there's something unique to $_SESSION ( Duh ), or I've munged something

obvious that's just become invisible to me through too much looking at code (Duh**2).

 

Now, if I could screw something up badly by using $_POST for awhile, please feel free

to berate me soundly, just in case.

 

Mark

 

Link to comment
Share on other sites

It's called in preceding code.  Do I need to call it in my function as well?  Would

that initialize the structure and run the risk of breaking the upper level code?

I'll give that a shot.

 

Do other superglobals, such as $_POST, have similar rules or support functions?

 

Thanks!

 

Mark

 

Link to comment
Share on other sites

It's in a lot of files.  But basically, Is there some general rule that would allow me to transfer

any value or variable from function A to function B and then have multiple forms call function B

again, saving that value?

 

You would THINK that a global variable would do it.  That choosing various names for that

variable would make collision improbable.  That $_SESSION, for example, would be one of

those variables.  That if the control never left my functions, regardless of what occurred

before or after those functions, that at least in MY functions, I would have access to that value.

 

But no.  Something is clobbering these values as though it's an entirely new execution context.

As though there's no way to keep a global value through repeated calls to Function B.

 

There's something probably terribly obvious that I'm missing (I did try calling start_session()

again at the top of each of the functions A and B, one at a time, and also in the calling

function -- no effect.)

 

Is there something in my test environment -- a windows PC running XP -- wherein repeated

calls might have some strange effect???  It's driving me absolutely NUTS.

 

Mark

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.