perrij3 Posted August 7, 2009 Share Posted August 7, 2009 I am having some issues with session variables. I want to pass the ID # from one page to the next. I have a from where people can enter a message. What the user enters is placed into a database. I want to be on the next page (the one that is displayed once a user submits the message) to display other information from the database and to be a link to different pages using the ID #. The issue is the ID # isn't being passed to the next page as far as I can tell. In the form on the first page, I use a hidden input box to record the ID # as such: <input name="O_ID" type="hidden" value="<?php echo $row['OB_ID']; ?>"> On the next page, I am trying to call that value (O_ID") using the following code: session_start(); //Check to see if the form has been submitted and that Obituary ID is not empty if ($_POST && !empty($_POST['O_ID'])) { //Set session variable $_SESSION['O_ID'] = $_POST['O_ID']; $O_ID = $_SESSION['O_ID']; //create database connection $conn = dbConnect(''); include('includes/connect.php'); $sql = "SELECT * FROM obituaries WHERE OB_ID = $O_ID"; $result = mysql_query($sql) or die (mysql_error()); $row = mysql_fetch_assoc($result); } Could someone please help me figure out where my error is at? Thanks for your help in advance. Quote Link to comment https://forums.phpfreaks.com/topic/169258-solved-session-variables/ Share on other sites More sharing options...
wildteen88 Posted August 7, 2009 Share Posted August 7, 2009 First check I'd make is to see if the hidden input field is being populated <?php echo 'Temp check, OB_ID = ' .$row['OB_ID']; ?> <input name="O_ID" type="hidden" value="<?php echo $row['OB_ID']; ?>"> Is it populated? If the field is populated. Is the $_POST populated correctly in the page the form gets submitted to <?php echo '<pre>'.print_r($_POST, true).'</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/169258-solved-session-variables/#findComment-893151 Share on other sites More sharing options...
perrij3 Posted August 7, 2009 Author Share Posted August 7, 2009 The hidden input field is being populated with the correct ID number and all the data that is being collected from the form is posting correctly to the database. Quote Link to comment https://forums.phpfreaks.com/topic/169258-solved-session-variables/#findComment-893155 Share on other sites More sharing options...
Psycho Posted August 7, 2009 Share Posted August 7, 2009 This is not a SESSION variable error. You are not using the session variable in the code - only setting it. 1. Are you getting any errors? 2. Have you checked the generated HTML of the form to ensure the hidden field does have a value 3. Verify the POST values on the processing page by adding this before the if statement: echo "<pre>"; echo print_r($_POST); echo "</pre>"; EDIT: The hidden input field is being populated with the correct ID number and all the data that is being collected from the form is posting correctly to the database. So, what IS the problem? If all the POST data is available on the processing page, then the ID number is there, right? Quote Link to comment https://forums.phpfreaks.com/topic/169258-solved-session-variables/#findComment-893157 Share on other sites More sharing options...
perrij3 Posted August 7, 2009 Author Share Posted August 7, 2009 I do not get any error messages when I run the code. I entered the print_r($_POST); code before my if statement and got this Array ( ) 1 So the problem has to be with posting the data. Do I have to add something in the ACTION part of the FORM to POST the data to the next page? Here is my PHP code and the FORM code for the page people enter the data on: <?php include('includes/connection.inc.php'); include('includes/corefuncs.php'); //Remove backslashes nukeMagicQuotes(); //initialize flag $done = false; //prepare an array of expected items $expected = array('Obituary_ID', 'FullName', 'EmailAddress', 'AddressField1', 'AddressField2', 'YourCity', 'YourState', 'YourZip', 'Comment', 'DateEntered'); //create database connection $conn = dbConnect(''); //get details of selected record if ($_GET && !$_POST) { if (isset($_GET['Obituary_ID']) && is_numeric($_GET['Obituary_ID'])) { $Obituary_ID = $_GET['Obituary_ID']; } else { $Obituary_ID = NULL; } if ($Obituary_ID) { $sql = "SELECT * FROM obituaries WHERE OB_ID = $Obituary_ID"; $result = mysql_query($sql) or die (mysql_error()); $row = mysql_fetch_assoc($result); } } //if form has been submitted, add record to guestbook if (array_key_exists('sign', $_POST)) { //prepare expected items for insertion into database foreach ($_POST as $key => $value) { if (in_array($key, $expected)) { ${$key} = mysql_real_escape_string($value); } } //abandon the process if primary key invalid if (!is_numeric($Obituary_ID)) { die('Invalid Request'); } //prepare the SQL query $sql = "INSERT INTO guestbook (`FK_OB_ID`, `Name`, `Email`, `Address1`, `Address2`, `City`, `State`, `Zip`, `Condolence`, `DateEntered`) VALUES ('$Obituary_ID', '$FullName', '$EmailAddress', '$AddressField1', '$AddressField2', '$YourCity', '$YourState', '$YourZip', '$Comment', NOW())"; //Submit the query $done = mysql_query($sql) or die(mysql_error()); //redirect page of $Obituary_ID is invalid if ($done || !isset($Obituary_ID)) { header('Location: /throops/thanks_guestbook.php'); exit; } } ?> </head> <body> <!-- Start Main Content --> <!-- Form to add a new entry to the guestbook --> <form id="guestbook" name="guestbook" method="POST" action=""> <table> <tr> <td class="tablefield"><label for="FullName"><strong>Name:</strong></label></td> <td class="tabledata"><input type="text" maxlength="100" size="30" type="text" name="FullName" tabindex="1" id="FullName"></td> </tr> <tr> <td class="tablefield"><label for="EmailAddress"><strong>Email:</strong></label></td> <td class="tabledata"><input type="text" maxlength="100" size="30" type="text" name="EmailAddress" tabindex="1" id="EmailAddress"></td> </tr> <tr> <td class="tablefield"><label for="AddressField1">Address 1:</label></td> <td class="tabledata"><input type="text" maxlength="100" size="30" type="text" name="AddressField1" tabindex="2" id="AddressField1"></td> </tr> <tr> <td class="tablefield"><label for="AddressField2">Address 2:</label></td> <td class="tabledata"><input type="text" maxlength="100" size="30" type="text" name="AddressField2" tabindex="3" id="AddressField2"></td> </tr> <tr> <td class="tablefield"><label for="YourCity">City:</label></td> <td class="tabledata"><input type="text" maxlength="50" size="30" type="text" name="YourCity" tabindex="4" id="YourCity"></td> </tr> <tr> <td class="tablefield"><label for="YourState">State:</label></td> <td class="tabledata"><select name="YourState" id="YourState" tabindex="5"> <option value="WY">Wyoming</option> </select> </td> </tr> <tr> <td class="tablefield"><label for="YourZip">Zip:</label></td> <td class="tabledata"><input type="text" maxlength="10" size="15" type="text" name="YourZip" tabindex="6" id="YourZip"></td> </tr> <tr> <td class="tablefield"><label for="Comment">Condolence Message:</label></td> <td class="tabledata"><textarea name="Comment" cols="40" rows="12" id="Comment" tabindex="11"></textarea></td> </tr> <tr> <td><input name="Obituary_ID" type="hidden" value="<?php echo $row['OB_ID']; ?>"></td> <td align="right"><input type="submit" name="sign" Value="Sign Guestbook"> </td> </tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/169258-solved-session-variables/#findComment-893165 Share on other sites More sharing options...
wildteen88 Posted August 7, 2009 Share Posted August 7, 2009 Do I have to add something in the ACTION part of the FORM to POST the data to the next page? if you dont set the action the form will submit to itself. You only need to set the action if you're submitting the form to different page. I entered the print_r($_POST); code before my if statement and got this Array ( ) 1 Is that before or after you submit the form? $_POST will be empty if you don't submit the form. You show add in the code I suggested, fill in your form and submit it. It should print the contents of the $_POST array. Quote Link to comment https://forums.phpfreaks.com/topic/169258-solved-session-variables/#findComment-893166 Share on other sites More sharing options...
perrij3 Posted August 7, 2009 Author Share Posted August 7, 2009 I added this code <?php echo '<pre>'.print_r($_POST, true).'</pre>'; ?> on the very top of the second page before i start the session variable. The data that I enter is being posted to the database, but not to the next page. Quote Link to comment https://forums.phpfreaks.com/topic/169258-solved-session-variables/#findComment-893178 Share on other sites More sharing options...
wildteen88 Posted August 7, 2009 Share Posted August 7, 2009 Oh hang on I just saw these lines in your code //redirect page of $Obituary_ID is invalid if ($done || !isset($Obituary_ID)) { header('Location: /throops/thanks_guestbook.php'); exit; } } You wont be able to access your $_POST variables in thanks_guestbook.php. $_POST will be only available to the page you submit the form to. If you want to use the $_POST data in that page you'll have to save the variables you want to carry using sessions. Change this line include('includes/connection.inc.php'); to session_start(); // must be called whenever you use $_SESSION's include('includes/connection.inc.php'); Now change header('Location: /throops/thanks_guestbook.php'); to $_SESSION['post'] = $_POST; header('Location: /throops/thanks_guestbook.php'); Now in thanks_guestbook.php make sure you call session_start() at the top of script. Use $_SESSION['post'] to access the $_POST data Quote Link to comment https://forums.phpfreaks.com/topic/169258-solved-session-variables/#findComment-893182 Share on other sites More sharing options...
Danny620 Posted August 7, 2009 Share Posted August 7, 2009 oh btw i would'tn use include use require insead Quote Link to comment https://forums.phpfreaks.com/topic/169258-solved-session-variables/#findComment-893184 Share on other sites More sharing options...
wildteen88 Posted August 7, 2009 Share Posted August 7, 2009 oh btw i would'tn use include use require insead That doesnt help the situation. They both do they same thing. The only difference is require will kill the script if it cant include the requested file. Quote Link to comment https://forums.phpfreaks.com/topic/169258-solved-session-variables/#findComment-893187 Share on other sites More sharing options...
perrij3 Posted August 7, 2009 Author Share Posted August 7, 2009 I changed the lines of code, but still nothing. Here is the new code, maybe that will help. I took out some coding that deals with SQL as well to make it shorter and easier to read. <?php session_start(); // must be called whenever you use $_SESSION's require('includes/connection.inc.php'); require('includes/corefuncs.php'); //Remove backslashes nukeMagicQuotes(); //initialize flag $done = false; //get details of selected record if ($_GET && !$_POST) { if (isset($_GET['Obituary_ID']) && is_numeric($_GET['Obituary_ID'])) { $Obituary_ID = $_GET['Obituary_ID']; } else { $Obituary_ID = NULL; } if ($Obituary_ID) { $sql = "SELECT * FROM obituaries WHERE OB_ID = $Obituary_ID"; $result = mysql_query($sql) or die (mysql_error()); $row = mysql_fetch_assoc($result); } } //if form has been submitted, add record to guestbook if (array_key_exists('sign', $_POST)) { //prepare expected items for insertion into database foreach ($_POST as $key => $value) { if (in_array($key, $expected)) { ${$key} = mysql_real_escape_string($value); } } //abandon the process if primary key invalid if (!is_numeric($Obituary_ID)) { die('Invalid Request'); } //redirect page of $Obituary_ID is invalid if ($done || !isset($Obituary_ID)) { $_SESSION['post'] = $_POST; header('Location: /throops/thanks_guestbook.php'); exit; } } ?> The next page starts like this: <?php echo '<pre>'.print_r($_POST, true).'</pre>'; ?> <?php //Start a session to be able to post name of whom the obituary is for on the thank you page session_start(); //Check to see if the form has been submitted and that Obituary ID is not empty if ($_POST && !empty($_POST['Obituary_ID'])) { //Set session variable $_SESSION['Obituary_ID'] = $_POST['Obituary_ID']; $Obituary_ID = $_SESSION['Obituary_ID']; Quote Link to comment https://forums.phpfreaks.com/topic/169258-solved-session-variables/#findComment-893192 Share on other sites More sharing options...
wildteen88 Posted August 7, 2009 Share Posted August 7, 2009 In your next page you need to use $_SESSION['post'] instead of $_POST So instead of $_SESSION['Obituary_ID']; you'd use $_SESSION['post']['Obituary_ID']; Quote Link to comment https://forums.phpfreaks.com/topic/169258-solved-session-variables/#findComment-893195 Share on other sites More sharing options...
perrij3 Posted August 7, 2009 Author Share Posted August 7, 2009 I would first like to thank everyone for all the help they have provide me so far. I am still not getting any data passed to the second page. This is still what I am getting when I am using <?php echo '<pre>'.print_r($_POST, true).'</pre>'; ?> at the top of the second page. Array ( ) I do make the changes to the second page to look like this: <?php //Start a session to be able to post name of whom the obituary is for on the thank you page session_start(); //Check to see if the form has been submitted and that Obituary ID is not empty include('includes/connection.inc.php'); if ($_POST && !empty($_SESSION['post']['Obituary_ID'])) { //Set session variable $_SESSION['post']['Obituary_ID'] = $_SESSION['post']['Obituary_ID']; $Obituary_ID = $_SESSION['post']['Obituary_ID']; Quote Link to comment https://forums.phpfreaks.com/topic/169258-solved-session-variables/#findComment-893204 Share on other sites More sharing options...
wildteen88 Posted August 7, 2009 Share Posted August 7, 2009 This if ($_POST && !empty($_SESSION['post']['Obituary_ID'])) { needs to be echo '<pre>'.print_r($_SESSION['post'], true).'</pre>'; if (isset($_SESSION['post']['Obituary_ID']) && !empty($_SESSION['post']['Obituary_ID'])) { Quote Link to comment https://forums.phpfreaks.com/topic/169258-solved-session-variables/#findComment-893205 Share on other sites More sharing options...
perrij3 Posted August 7, 2009 Author Share Posted August 7, 2009 THAT WORKED!!! THANK YOU SO MUCH!! Quote Link to comment https://forums.phpfreaks.com/topic/169258-solved-session-variables/#findComment-893211 Share on other sites More sharing options...
mikesta707 Posted August 7, 2009 Share Posted August 7, 2009 isnt isset($_SESSION['post']['Obituary_ID']) and !empty($_SESSION['post']['Obituary_ID']) the same thing? EDIT: Nevermind Quote Link to comment https://forums.phpfreaks.com/topic/169258-solved-session-variables/#findComment-893213 Share on other sites More sharing options...
wildteen88 Posted August 7, 2009 Share Posted August 7, 2009 No, isset checks whether $_SESSION['post']['Obituary_ID'] is defined. empty checks whether the variable is emtpy. I always use isset before using _GET, _POST, _COOKIE or _SESSION variables. Quote Link to comment https://forums.phpfreaks.com/topic/169258-solved-session-variables/#findComment-893223 Share on other sites More sharing options...
mikesta707 Posted August 7, 2009 Share Posted August 7, 2009 No, isset checks whether $_SESSION['post']['Obituary_ID'] is defined. empty checks whether the variable is emtpy. I always use isset before using _GET, _POST, _COOKIE or _SESSION variables. yeah I realized right as I posted, srry Quote Link to comment https://forums.phpfreaks.com/topic/169258-solved-session-variables/#findComment-893224 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.