saco721 Posted April 24, 2013 Share Posted April 24, 2013 Hi, I am new to php and am having problems with sessions, basically I have a form that I have split into 2 pages. I use session variables to collect the input from both pages and then output the values to a final page that will eventually email the submissions to me. This part works fine, I get all the desired information as required. The problem I am having is that I am trying to make each page retain values as well as using session variables, so if a user decides to go back to make changes, they only have to change the desired field rather than having to re-enter values in all the fields. When I try to go back, all the fields are empty. I have tried using header instead of meta refresh and it still does the same thing. The only way I can get it to work is if I move <?phpsession_start();$_SESSION['clientname'] = $_POST['name']; //name$_SESSION['clientcompany'] = $_POST['company']; //company?> and <?phpsession_start();$_SESSION['productname'] = $_POST['product']; //name$_SESSION['sizename'] = $_POST['size']; //company?> from the top of the script to next block of php code. The the problem is that everything works but I have to reload the page before I get the session variables to populate. here is the code : <?php session_start(); $_SESSION['clientname'] = $_POST['name']; //name $_SESSION['clientcompany'] = $_POST['company']; //company ?> <html> <head> <title>Page 1</title> </head> <body> <?php // move session stuff to here, works fine but reload of page needed. $N = ''; $C = ''; if(isset($_POST['submit'])) { //process the text-input field if(isset($_POST['name'])) $N = $_POST['name']; else $N = false; if(isset($_POST['submit'])) if(isset($_POST['submit'])) //process the text-input field if(isset($_POST['company'])) $C = $_POST['company']; else $C = false; //if all entries okay, process information, display message and exit script. if ($N && $C) { echo '<META HTTP-EQUIV="Refresh" Content="0; URL=size.php">'; //exit; echo '</body></html>'; exit(); } } echo '<h2>Page 1</h2>'; echo '<form name="Form1" action="' . $_SERVER['PHP_SELF'] . '" method="POST">'; //display text-input label for name if(isset($_POST['submit']) && !$N) echo '<p><font color="red" size="+1"><b>Please Specify</b></font><br />'; else echo '<p>Please enter Name [required]<br />'; //display the text-input field echo '<input type="text" name="name" maxlength="60" size="60" value="' . $_SESSION['clientname'] . '" /></p>'; if(isset($_POST['submit']) && !$C) echo '<p><font color="red" size="+1"><b>Please Specify</b></font><br />'; else echo '<p>Please enter Company [required]<br />'; //display the text-input label for company echo '<input type="text" name="company" maxlength="60" size="60" value="' . $_SESSION['clientcompany'] . '" /></p>'; echo '<p><input type="submit" name="submit" value="submit"></p>'; echo '</form>'; echo '</body>'; echo '</html>'; and size.php : <?php session_start(); $_SESSION['productname'] = $_POST['product']; //name $_SESSION['sizename'] = $_POST['size']; //company ?> <html> <head> <title>Page 2</title> </head> <body> <?php // move session stuff to here, works fine but reload of page needed. $P = ''; $C = ''; if(isset($_POST['submit'])) { //process the text-input field if(isset($_POST['product'])) $P = $_POST['product']; else $P = false; if(isset($_POST['submit'])) if(isset($_POST['submit'])) //process the text-input field if(isset($_POST['size'])) $SI = $_POST['size']; else $SI = false; //if all entries okay, process information, display message and exit script. if ($P && $SI) { echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php">'; //exit; echo '</body></html>'; exit(); } } echo '<h2>Page 2</h2>'; echo '<form name="Form1" action="' . $_SERVER['PHP_SELF'] . '" method="POST">'; //display text-input label for name if(isset($_POST['submit']) && !$P) echo '<p><font color="red" size="+1"><b>Please Specify</b></font><br />'; else echo '<p>Please enter Product Name [required]<br />'; //display the text-input field echo '<input type="text" name="product" maxlength="60" size="60" value="' . $_SESSION['productname'] . '" /></p>'; if(isset($_POST['submit']) && !$SI) echo '<p><font color="red" size="+1"><b>Please Specify</b></font><br />'; else echo '<p>Please enter Size [required]<br />'; //display the text-input label for company echo '<input type="text" name="size" maxlength="60" size="60" value="' . $_SESSION['sizename'] . '" /></p>'; echo '<p><input type="submit" name="submit" value="submit"></p>'; echo '</form>'; echo '</body>'; echo '</html>'; and success.php : <?php session_start(); echo 'product type is : '; echo $_SESSION['productname']; echo "<br>"; echo 'Client Name : '; echo $_SESSION['clientname']; echo "<br>"; echo 'Client Company : '; echo $_SESSION['clientcompany']; echo "<br>"; session_end(); ?> sorry about the size of the scripts, I have reduced them as much as I can. Any help would be greatly appreciated. Thank you. Quote Link to comment Share on other sites More sharing options...
lemmin Posted April 24, 2013 Share Posted April 24, 2013 You should definitely be using header() for something like that. You should have your script go through all of the logic before outputting any information. This allows you to completely change the location without going out of the way to send information to the browser to tell it to change pages. Quote Link to comment Share on other sites More sharing options...
DavidAM Posted April 24, 2013 Share Posted April 24, 2013 <?php session_start(); $_SESSION['clientname'] = $_POST['name']; //name $_SESSION['clientcompany'] = $_POST['company']; //company ?> When you come to the page by a redirect, $_POST will not exist. So this code will trigger a warning and then assign NULL to your $_SESSION variables. You need to test for $_POST. If the $_POST fields are not set, then keep the current session values. To make your form "sticky" you need to apply the $_SESSION values in the INPUT tags (looks like you have that). Of course, on the first time visit to a page, the $_SESSION values will not exist, and referencing them will trigger a warning. So we have to check for them as well. So, something like this: <?php session_start(); $_SESSION['clientname'] = (isset($_POST['name']) ? $_POST['name'] : (isset($_SESSION['clientname']) ? $_SESSION['clientname'] : '')); //name $_SESSION['clientcompany'] = (isset($_POST['company']) ? $_POST['company'] : (isset($_SESSION['clientcompany']) ? $_SESSION['clientcompany'] : '')); //company ?>which is functionally equivalent to: session_start(); if (isset($_POST['name'])) $_SESSION['clientname'] = $_POST['name']; elseif (! isset($_SESSION['clientname'])) $_SESSION['clientname'] = ''; if (isset($_POST['company'])) $_SESSION['clientcompany'] = $_POST['company']; elseif (! isset($_SESSION['clientcompany'])) $_SESSION['clientcompany'] = ''; # Now we can refer to $_SESSION without concern of how we got here. Of course, you still need to sanitize and escape the data when you use it. Quote Link to comment Share on other sites More sharing options...
saco721 Posted May 7, 2013 Author Share Posted May 7, 2013 Hey lemmin and DavidAM, I incorporated your suggestions about header instead of meta refresh and checking for $_POST and $_SESSION variables, scripts now work fine. Thank you very much for your help, it helped me out greatly! Quote Link to comment Share on other sites More sharing options...
saco Posted May 15, 2013 Share Posted May 15, 2013 Hi, I should have included this with my initial post, but I now have a similar problem with radio buttons, I need the values to be retained after leaving and returning to the form (sticky radio buttons), the values are then referenced using session variables and emailed to me when the form is complete. The text field aspect is working fine. Any help would be greatly appreciated. Here is the code : <?php session_start(); $_SESSION['otherchoice'] = (isset($_POST['choice']) ? $_POST['choice'] : (isset($_SESSION['otherchoice']) ? $_SESSION['otherchoice'] : '')); //radio $_SESSION['textchoice'] = (isset($_POST['txt']) ? $_POST['txt'] : (isset($_SESSION['textchoice']) ? $_SESSION['textchoice'] : '')); //text //$_SESSION['otherchoice'] = $_POST['choice']; //radio //$_SESSION['textchoice'] = $_POST['txt']; //text $OC = ''; if(isset($_POST['submit'])) { //process the radio buttons if(isset($_POST['choice'])) $OC = $_POST['choice']; else $OC = false; if($OC == 'yes' && ($_SESSION['textchoice'] == '')) $OC = false; else $OC = $_POST['choice']; //echo '<p>Other Choice<br />'; //if all entries okay, process information, display message and exit script. if($OC) { header("Location: success2.php"); } } ?> <html> <head> <title>Page 3</title> <head> <script type="text/javascript"> function make_blank() { document.theform.txt.value =""; } </script> <script type="text/javascript"> function make_blank2() { document.theform.txt2.value =""; } </script> </head> <body> <?php echo '<h2>Page 3</h2>'; echo '<form name="theform" action="' . $_SERVER['PHP_SELF'] . '" method="POST">'; if(isset($_POST['submit']) && !$OC) echo '<p><font color="red" size="+1"><b>Please Specify</b></font><br />'; else echo '<p>What colour would you like? [required]<br />'; if($OC == 'red') echo '<input type="radio" name="choice" value="red" checked onClick="make_blank();this.form.txt.disabled=true;"/>red <br />'; else echo '<input type="radio" name="choice" value="red" onClick="make_blank();this.form.txt.disabled=true;"/>red <br />'; if($OC == 'green') echo '<input type="radio" name="choice" value="green" checked onClick="make_blank();this.form.txt.disabled=true;"/>green <br />'; else echo '<input type="radio" name="choice" value="green" onClick="make_blank();this.form.txt.disabled=true;"/>green <br />'; if($OC == 'orange'){ echo '<input type="radio" name="choice" value="orange" checked onClick="make_blank();this.form.txt.disabled=true;"/>orange <br />'; }else{ echo '<input type="radio" name="choice" value="orange" onClick="make_blank();this.form.txt.disabled=true;"/>orange <br />'; } if($OC == 'other') echo '<input type="radio" name="choice" value="other" onClick="this.form.txt.disabled=false;this.form.txt.focus()"checked />Other...Please specify '; else echo '<input type="radio" name="choice" value="other" onClick="this.form.txt.disabled=false;this.form.txt.focus();"/>Other...Please specify '; echo '<input disabled type="text" name="txt" value="' . $_SESSION['textchoice'] .'" />'; echo '<p><input type="submit" name="submit" value="submit"></p>'; echo '</form>'; echo '</body>'; echo '</html>'; ?> and success2.php is : <?php session_start(); echo 'What colour would you like? : '; if ($_SESSION['otherchoice'] == "other"){ echo "other"; echo "<br>"; echo 'Other information is : '; echo $_SESSION['textchoice']; echo "<br>";} else { echo $_SESSION['otherchoice']; echo "<br>";} ?> thank you! Quote Link to comment Share on other sites More sharing options...
saco Posted May 15, 2013 Share Posted May 15, 2013 Hi, just to say that I resolved the problem. I was checking local variables instead of session variables. Thanks for your help! Quote Link to comment 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.