gmark Posted February 16, 2011 Share Posted February 16, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/227901-save-values-in-new-form-scope/ Share on other sites More sharing options...
KevinM1 Posted February 16, 2011 Share Posted February 16, 2011 Are you using session_start at the beginning of each of your files? Quote Link to comment https://forums.phpfreaks.com/topic/227901-save-values-in-new-form-scope/#findComment-1175153 Share on other sites More sharing options...
gmark Posted February 16, 2011 Author Share Posted February 16, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/227901-save-values-in-new-form-scope/#findComment-1175281 Share on other sites More sharing options...
KevinM1 Posted February 16, 2011 Share Posted February 16, 2011 Show your code. Quote Link to comment https://forums.phpfreaks.com/topic/227901-save-values-in-new-form-scope/#findComment-1175283 Share on other sites More sharing options...
gmark Posted February 16, 2011 Author Share Posted February 16, 2011 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/227901-save-values-in-new-form-scope/#findComment-1175291 Share on other sites More sharing options...
gmark Posted February 16, 2011 Author Share Posted February 16, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/227901-save-values-in-new-form-scope/#findComment-1175321 Share on other sites More sharing options...
Pikachu2000 Posted February 16, 2011 Share Posted February 16, 2011 When posting code, please enclose it within the forum's . . . BBCode tags. Quote Link to comment https://forums.phpfreaks.com/topic/227901-save-values-in-new-form-scope/#findComment-1175328 Share on other sites More sharing options...
KevinM1 Posted February 16, 2011 Share Posted February 16, 2011 Like I said before, you NEED to use session_start at the top of each PHP file you want to access $_SESSION with. Otherwise, the session will not continue to exist in those files. It needs to be one of the very first lines of code you write. Not in a conditional, not in a loop, but right away in each file. Quote Link to comment https://forums.phpfreaks.com/topic/227901-save-values-in-new-form-scope/#findComment-1175351 Share on other sites More sharing options...
gmark Posted February 17, 2011 Author Share Posted February 17, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/227901-save-values-in-new-form-scope/#findComment-1175422 Share on other sites More sharing options...
KevinM1 Posted February 17, 2011 Share Posted February 17, 2011 Is all the code in one file, or split up into multiple files? It's hard to tell since you didn't use code tags. Quote Link to comment https://forums.phpfreaks.com/topic/227901-save-values-in-new-form-scope/#findComment-1175423 Share on other sites More sharing options...
gmark Posted February 17, 2011 Author Share Posted February 17, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/227901-save-values-in-new-form-scope/#findComment-1175814 Share on other sites More sharing options...
gmark Posted February 17, 2011 Author Share Posted February 17, 2011 I see that the values -- _POST, _SESSION, global variables, never survive the first FORM. Perhaps a new call to the main function initializes the entire environment? Is that any clue? Mark Quote Link to comment https://forums.phpfreaks.com/topic/227901-save-values-in-new-form-scope/#findComment-1175824 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.