tlavelle Posted August 16, 2007 Share Posted August 16, 2007 I have this code. It contains and process 2 forms and is supposed to talk to a page that generates results <?php //initialize session session_start(); ?> <html> <head> <basefont face="Arial"> </head> <body> <?php // set server access variables $host = "localhost"; $user = "root"; $pass = ""; $db = "wercbench"; $useryear=$_POST['year']; // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // create form 1 queries $year_query = "SELECT distinct year FROM userdata"; $industry_query = "SELECT distinct comp_desc FROM comp_desc"; // execute form 1 queries $year_result = mysql_query($year_query) or die ("Error in query: $query. ".mysql_error()); $industry_result = mysql_query($industry_query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($year_result) > 0 or mysql_num_rows($industry_result)){ // yes // print them one after another if (!isset($_POST['wercbench1']) and !isset($_POST['year'])) { /*" name=\"wercbench1\">";*/ echo"<form method=\"POST\" action=\"". $_SERVER['PHP_SELF']. "\" name=\"wercbench1\">"; echo"<table style=\"width: 100%;\" border=\"1\" cellpadding=\"2\" cellspacing=\"2\">"; echo"<tbody>"; echo" <tr>"; echo" <td>Select the comparison year and group then click next"; echo" </td>"; echo" <td></td>"; echo" </tr>"; echo" <tr>"; echo" <td>"; echo" <select name=\"year\">"; while($row = mysql_fetch_row($year_result)) { echo"<option value=\"" .$row[0]. "\">".$row[0]."</option>"; } echo" </select>"; echo" </td>"; echo" <td></td>"; echo" </tr>"; echo" <tr>"; echo" <td>"; echo" <select name=\"industry\">"; while($row = mysql_fetch_row($industry_result)) { echo"<option value=\"" .$row[0]. "\">".$row[0]."</option>"; } echo" </select>"; echo"</td>"; echo" <td></td>"; echo" </tr>"; echo" <tr>"; echo" <td><input type=\"Submit\" value=\"Next\" name=\"wercbench1_next\"></button></td>"; echo" <td></td>"; echo" </tr>"; echo"</tbody>"; echo"</table>"; echo"</form>"; } } else { // no // print status message echo "No rows found!"; } //Calcualtor variables if(isset($_POST['wercbench1_next'])) { $_Session['sess_year']=$_POST['year']; $userindustry=$_POST['industry']; $_Session['sess_ind']=$_POST['industry']; $metric_query = "SELECT metric_desc.met_desc, userdata.median, userdata.best_pract FROM metric_desc, userdata where metric_desc.met_key=userdata.met_key and $useryear=userdata.year"; $metric_result = mysql_query($metric_query) or die ("Error in query: $query. ".mysql_error()); echo $_Session['sess_year']; echo $useryear; if(!isset($_POST['wercbench2']) and (mysql_num_rows($metric_result)) > 0){ // yes // print them one after another //echo "<form action=\"results.php\" method=\"post\" name\"wercbench2\">"; echo "<form action=\"sesstest.php\" method=\"post\" name\"wercbench2\">"; echo "<table cellpadding=10 border=1>"; echo "<tr>"; echo "<td>Metric Description</td>"; echo "<td>Average</td>"; echo "<td>Best Practice</td>"; echo "<td>Enter your value here</td>"; echo "</tr>"; while($row = mysql_fetch_row($metric_result)) { echo "<tr>"; echo "<td>".$row[0]."</td>"; echo "<td>" . $row[1]."</td>"; echo "<td>".$row[2]."</td>"; echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"\" /></td>"; echo "</tr>"; } echo "<tr><td colspan=\"3\"> </td><td align=\"center\"><input type=\"submit\" name=\"wercbench_next2\" value=\"Compare\"></td></tr></table>"; } /* if(!isset($_POST['wercbench_next2']) and (mysql_num_rows($metric_result)) > 0){ $_SESSION['Posted_values'] = array(); foreach($_POST as $fld => $val) $_SESSION['Posted_values'][$fld] = $val; echo '<pre>' . print_r($_SESSION,true) . '</pre>'; } */ // free result set memory mysql_free_result($year_result); mysql_free_result($industry_result); mysql_free_result($metric_result); //mysql_free_result($metric_result); } // close connection mysql_close($connection); ?> </body> </html> I created this page just to try to debut <html> <head> <basefont face="Arial"> </head> <body> <?php echo $_Session['sess_year']; ?> </body> </html> Why am i not getting the session variable value on the second page? Quote Link to comment https://forums.phpfreaks.com/topic/65299-why-is-my-session-variable-not-transferring-between-these-2-pages/ Share on other sites More sharing options...
Anzeo Posted August 16, 2007 Share Posted August 16, 2007 You need this code at the top of every page you whish your session to be transferred to: session_start(); header("Cache-control: private"); Quote Link to comment https://forums.phpfreaks.com/topic/65299-why-is-my-session-variable-not-transferring-between-these-2-pages/#findComment-326086 Share on other sites More sharing options...
tlavelle Posted August 16, 2007 Author Share Posted August 16, 2007 what does the header control private do? Quote Link to comment https://forums.phpfreaks.com/topic/65299-why-is-my-session-variable-not-transferring-between-these-2-pages/#findComment-326088 Share on other sites More sharing options...
nathanmaxsonadil Posted August 16, 2007 Share Posted August 16, 2007 you need to start the session on the page you are going to echo it on too <html> <head> <basefont face="Arial"> </head> <body> <?php echo $_Session['sess_year']; ?> </body> </html> should be <?php session_start(); echo "<html> <head> <basefont face='Arial'> </head> <body> {$_Session['sess_year']}; </body> </html>"; ?> Sorry now it should work Quote Link to comment https://forums.phpfreaks.com/topic/65299-why-is-my-session-variable-not-transferring-between-these-2-pages/#findComment-326093 Share on other sites More sharing options...
tlavelle Posted August 16, 2007 Author Share Posted August 16, 2007 That is not working Quote Link to comment https://forums.phpfreaks.com/topic/65299-why-is-my-session-variable-not-transferring-between-these-2-pages/#findComment-326097 Share on other sites More sharing options...
Anzeo Posted August 16, 2007 Share Posted August 16, 2007 That's not right Nathan, I use this, I'm not sure wether the cache-control is needed or not. <?php session_start(); header("Cache-control: private"); ?> <html> <head> <basefont face="Arial"> </head> <body> <?php echo $_Session['sess_year']; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/65299-why-is-my-session-variable-not-transferring-between-these-2-pages/#findComment-326102 Share on other sites More sharing options...
tlavelle Posted August 16, 2007 Author Share Posted August 16, 2007 so session_start() needs to be on top of each page in the application? Quote Link to comment https://forums.phpfreaks.com/topic/65299-why-is-my-session-variable-not-transferring-between-these-2-pages/#findComment-326123 Share on other sites More sharing options...
nathanmaxsonadil Posted August 16, 2007 Share Posted August 16, 2007 so session_start() needs to be on top of each page in the application? yes Quote Link to comment https://forums.phpfreaks.com/topic/65299-why-is-my-session-variable-not-transferring-between-these-2-pages/#findComment-326129 Share on other sites More sharing options...
tlavelle Posted August 16, 2007 Author Share Posted August 16, 2007 This still isnt working page 1 <?php //initialize session session_start(); header("Cache-control: private"); ?> <html> <head> <basefont face="Arial"> </head> <body> <?php // set server access variables $host = "localhost"; $user = "root"; $pass = ""; $db = "wercbench"; $useryear=$_POST['year']; // open connection $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); // select database mysql_select_db($db) or die ("Unable to select database!"); // create form 1 queries $year_query = "SELECT distinct year FROM userdata"; $industry_query = "SELECT distinct comp_desc FROM comp_desc"; // execute form 1 queries $year_result = mysql_query($year_query) or die ("Error in query: $query. ".mysql_error()); $industry_result = mysql_query($industry_query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($year_result) > 0 or mysql_num_rows($industry_result)){ // yes // print them one after another if (!isset($_POST['wercbench1']) and !isset($_POST['year'])) { /*" name=\"wercbench1\">";*/ echo"<form method=\"POST\" action=\"". $_SERVER['PHP_SELF']. "\" name=\"wercbench1\">"; echo"<table style=\"width: 100%;\" border=\"1\" cellpadding=\"2\" cellspacing=\"2\">"; echo"<tbody>"; echo" <tr>"; echo" <td>Select the comparison year and group then click next"; echo" </td>"; echo" <td></td>"; echo" </tr>"; echo" <tr>"; echo" <td>"; echo" <select name=\"year\">"; while($row = mysql_fetch_row($year_result)) { echo"<option value=\"" .$row[0]. "\">".$row[0]."</option>"; } echo" </select>"; echo" </td>"; echo" <td></td>"; echo" </tr>"; echo" <tr>"; echo" <td>"; echo" <select name=\"industry\">"; while($row = mysql_fetch_row($industry_result)) { echo"<option value=\"" .$row[0]. "\">".$row[0]."</option>"; } echo" </select>"; echo"</td>"; echo" <td></td>"; echo" </tr>"; echo" <tr>"; echo" <td><input type=\"Submit\" value=\"Next\" name=\"wercbench1_next\"></button></td>"; echo" <td></td>"; echo" </tr>"; echo"</tbody>"; echo"</table>"; echo"</form>"; } } else { // no // print status message echo "No rows found!"; } //Calcualtor variables if(isset($_POST['wercbench1_next'])) { $_Session['sess_year']=$_POST['year']; $userindustry=$_POST['industry']; $_Session['sess_ind']=$_POST['industry']; $metric_query = "SELECT metric_desc.met_desc, userdata.median, userdata.best_pract FROM metric_desc, userdata where metric_desc.met_key=userdata.met_key and $useryear=userdata.year"; $metric_result = mysql_query($metric_query) or die ("Error in query: $query. ".mysql_error()); echo $_Session['sess_year']; echo $useryear; if(!isset($_POST['wercbench2']) and (mysql_num_rows($metric_result)) > 0){ // yes // print them one after another //echo "<form action=\"results.php\" method=\"post\" name\"wercbench2\">"; echo "<form action=\"sesstest.php\" method=\"post\" name\"wercbench2\">"; echo "<table cellpadding=10 border=1>"; echo "<tr>"; echo "<td>Metric Description</td>"; echo "<td>Average</td>"; echo "<td>Best Practice</td>"; echo "<td>Enter your value here</td>"; echo "</tr>"; while($row = mysql_fetch_row($metric_result)) { echo "<tr>"; echo "<td>".$row[0]."</td>"; echo "<td>" . $row[1]."</td>"; echo "<td>".$row[2]."</td>"; echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"\" /></td>"; echo "</tr>"; } echo "<tr><td colspan=\"3\"> </td><td align=\"center\"><input type=\"submit\" name=\"wercbench_next2\" value=\"Compare\"></td></tr></table>"; } /* if(!isset($_POST['wercbench_next2']) and (mysql_num_rows($metric_result)) > 0){ $_SESSION['Posted_values'] = array(); foreach($_POST as $fld => $val) $_SESSION['Posted_values'][$fld] = $val; echo '<pre>' . print_r($_SESSION,true) . '</pre>'; } */ // free result set memory mysql_free_result($year_result); mysql_free_result($industry_result); mysql_free_result($metric_result); //mysql_free_result($metric_result); } // close connection mysql_close($connection); ?> </body> </html> Page 2 <?php //initialize session session_start(); header("Cache-control: private"); ?> <html> <head> <basefont face="Arial"> </head> <body> <?php echo $_Session['sess_year']; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/65299-why-is-my-session-variable-not-transferring-between-these-2-pages/#findComment-326135 Share on other sites More sharing options...
chocopi Posted August 16, 2007 Share Posted August 16, 2007 You should try reading this The Manual Try using <html> <head> <basefont face="Arial"> </head> <body> <?php session_start(); $sess_year = $_SESSION['sess_year']; echo $sess_year; ?> </body> </html> ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/65299-why-is-my-session-variable-not-transferring-between-these-2-pages/#findComment-326144 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.