Link to home
Start Free TrialLog in
Avatar of abrusko
abruskoFlag for United States of America

asked on

How do I retain the values in textareas when returning to the form with browser "back" button?

Hi,

I have a form which has several textareas for data.  I edit the data and when I find an error, I use ECHO to display a message.  The user should then hit the browser "back" button to return to the form to fix the data and resumit it, but when they hit "back" the form is refreshed and they must re-key the data.

I'm sure it's because of the session_start(); in the form...that is there because I have a logon screen that gets them to this form, so this form is coded like this:
<?php
session_start();
if(!session_is_registered(myusername))
   {
   header("location:CIIdeasFormMainLogin.php");
   }

Is there some way to preserve the data in the textareas in tihs scenario?

Thanks alot for your help!

Andy


Avatar of nplib
nplib
Flag of Canada image

if your just relying on the user to hit the back button on the web browser then no.

you could do one of several things,

you can use sessions, and store all data in session variables, but you need to remember to clean them after they are no longer needed.

you could use hidden fields on the page where people are hitting the back button, and put your own back button there that sends the data back.
Avatar of abrusko

ASKER

Thanks, nplib!  

If I store them in session variables, how would I put the values back into the textareas?

Can you give me a simple example of how to do that?
Avatar of Beverley Portlock
Something like

<textarea><?php echo htmlspecialchars( $_SESSION['variable_I_stored_my_data_in'],  ENT_QUOTES) ; ?></textarea>

should do it
ASKER CERTIFIED SOLUTION
Avatar of nplib
nplib
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of abrusko

ASKER

The page that's being submitted and the page with the textareas is the same page.

Thanks...I have ameeting now...I will work on this later and get back to you.

Thank you very much!
Avatar of abrusko

ASKER

nplib,

Not working.  Is it because the I have a form page that has the textares...then I use ECHO to display a message, then hit browser "back" and camo back into the original page?

How do I save something to a session variable and then retrieve it from that same page?

Thanks for bearing with me on this.

Andy
<?php
session_start();

$_SESSION['myvar'] = something


echo $_SESSION['myvar'];
Avatar of abrusko

ASKER

I'm not sure I'm explaining my scenario properly, cuz we're really not hitting on my exact problem.

Thank you very much for your help.  The info you have given me is valuable, but just not solving my specific problem.

I'll accept your answer so you can get the points cuz you spent some time with me...I appreciate it.

Andy
how about you post your code, then I can see what your problem is.
Avatar of abrusko

ASKER

That would be great...thanks...stay tuned...
Avatar of abrusko

ASKER

Before I post the code let me give you a recap of what I am attempting to do.

I have a form that is gotten to thru a logon form.  I'll post the code that I use to validate and direct the user to my main page.

After the user has reached the main page it should work like this:
It displays a form where the user can enter values in any number of textareas or input areas and I will use the values in those areas to build a query to retrieve data from an oracle table and display it back into the SAME form.

Here's the problem that I am trying to overcome right now:  When I edit the values that the user has entered into the textareas and input areas, I use ECHO to display a message about the field that needs to be corrected.  When the user then hits the browser BACK button to go back and correct and resubmit the form, any of the data from the textareas and input areas is lost and they must re-key it all.  I would like for the data to stay on the form so they can just make the correction and resubmit.

Anyway...thanks for any help you can give!!!

I'll attach the code...
Here's the login form:
 
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="CIIdeasFormCheckLogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Authorized User Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
 
 
Here's the vlidation and redirection code:
 
<?php
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 
 
$conn = OCILogon($myusername, $mypassword, 'CIDB') or die("ERROR CONNECTING - Notify Global Support");
 
header("location:CIIdeasFormSecure.php");
?>
 
 
And here's my main code:
 
<?php 
session_start();
if(!session_is_registered(myusername))
   {
   header("location:CIIdeasFormMainLogin.php");
   }
 
$signon=$_SESSION['myusername'];
$password=$_SESSION['mypassword'];
$teamname = $_POST["teamname"];
$formdate = $_POST["formdate"];
$ideanumber = $_POST["ideanumber"];
$mansource = $_POST["mansource"];
$ideamission = $_POST["ideamission"];
$affected = $_POST["affected"];
$impidea = $_POST["impidea"];
$currsit = $_POST["currsit"];
$newidea = $_POST["newidea"];
$factors = $_POST["factors"];
if (!isset($_POST['search'])) 
{ // if page is not submitted to itself echo the form
 
?>
<html>
<head><title>Continuous Improvement Ideas Form</title>
<img src="abcd_logo.jpg" align=left hspace=0><img src="abcd_logo.jpg" align=right hspace=0><br><br><P style="font-size: 24pt; color: green" align="center"><b>Continuous Improvement Ideas</b>
<style>
 body
 {
 background: <?php echo '#FFFF99';?>;
 }
</style>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>"> 
<hr noshade color=black>
<p style="text-align: center;"><font size=2><b>
COMPLETED FORMS ARE PROCESSED BY THE CONTINUOUS IMPROVEMENT GROUP
</font></b></p>
<hr size=4 noshade color=black>
 
<font size=4 color=red><b> 1) &nbsp;Mission Statement Section</b></font><br>
<font size=2 color=red><i>This section should outline the mission and goal of the idea.</i></font>
 
<table border=2 bordercolor=black >
 <tr>
  <td>
   <form method="post" action="<?php echo $PHP_SELF;?>">
   <font size=4 color=green><b>Date:&nbsp;
   <font size=4 color=black><b>
   <input type="text" size="11" maxlength="10" name="formdate">
 
   &nbsp;</font></b>
   <font size=4 color=green><b>&nbsp;&nbsp;&nbsp;&nbsp;Idea#:&nbsp;</font><font color=black></b>
   <input type="text" size="11" maxlength="10" name="ideanumber">
   </font><br /><br>
   <font size=4 color=green><b>Inintiator:&nbsp;&nbsp;</font></b>
   <input type="text" size="64" maxlength="64" name="initiator"><br>
 
   <br>
   <font size=4 color=green><b>Manufacturing Source:</font></b><br />
   <font color=#990099>Forks I:</font><input type="radio" value="Forks I" name="mansource"> &nbsp;&nbsp;&nbsp;&nbsp;
   <font color=#990099>Forks II:<input type="radio" value="Forks II" name="mansource">&nbsp;&nbsp;&nbsp;&nbsp;
   <font color=#990099>LVIP:<input type="radio" value="lvip" name="mansource">&nbsp;&nbsp;&nbsp;&nbsp;
   <font color=#990099>Distribution:<input type="radio" value="distribution" name="mansource">&nbsp;&nbsp;&nbsp;&nbsp;
   <font color=#990099>Other:<input type="radio" value="other" name="mansource">&nbsp;&nbsp;&nbsp;&nbsp;
   <br />
   <br>
   <font size=4 color=green><b>Idea Mission/Goal:</font></b><br />
   <textarea rows="5" cols="105" name="ideamission" wrap="soft"></textarea><br /> 
   <br>
   <font size=4 color=green><b>Product or Line Affected:&nbsp;</font></b><font size=2 color=green><i>
   (What specific product or production line will be affected by the CI event?)</font></i><br />
   <textarea rows="2" cols="105" name="affected" wrap="soft"></textarea><br /> 
   <br>
   <font size=4 color=green><b>Improvement Idea:</font></b><br />
   <textarea rows="5" cols="105" name="impidea" wrap="soft"></textarea><br /> 
   <br>
  </td>
 </tr>
</table>
 
<hr size=4 noshade color=black><br><br>
 
<font size=4 color=red><b> 2) &nbsp;Current Situation (AS IS)</b></font><br>
<font size=2 color=red><i>Fill out this section to establish a baseline to measure the improvement against.  This should be the "as-is".</i></font>
<p style="text-align: left;"><font size=2><b>Description:</font></b></p>
<textarea rows="7" cols="105" name="currsit" wrap="soft"></textarea><br /> 
<br><br>
<font size=4 color=red><b> 3) &nbsp;New Ideas (SHOULD BE)</b></font>
<p style="text-align: left;"><font size=2><b>Description:</font></b></p>
<textarea rows="7" cols="105" name="newidea" wrap="soft"></textarea><br /> 
<br><br>
 
 
   <font size=4 color=red><b> 4) &nbsp;Impacted Factors</b></font><br>
<?php
$conn = OCILogon($signon, $password, 'CIDB') or die("ERROR CONNECTING - Notify Global Support");
$query = "select literal_text from ci.literal where literal_group = 'Impacted Factors' order by seq_no";
$stid = OCI_Parse($conn, $query);
$r = OCI_Execute($stid, OCI_DEFAULT);
if ($r)
   {
   echo "<table border=0>";
   while ($row = OCI_FETCH_ARRAY ($stid, OCI_ASSOC))
      {
      echo "<TR bordercolor=#ffff99>";
      foreach ($row as $item)
         {
         echo "<TD width=858>" . "<font color=#990099>".
         "<input type='checkbox' value = '$item' name = 'factors[]'>" . $item .
         "</font>"."</td>";
         }  // End of FOREACH
      echo "</tr>";
      }  // End of While
   echo "</table>";
   }
else
   {
   echo 'Unable to execute query.  Notify Global Support.';
   }  // end of IF
OCILogoff($conn);
?>
 
<hr size=4 noshade color=black><br>
 
<input type="submit" value="Search" name="search"><br>
<font color=navy> Search for Values Entered in the Form Above.<br></font>
</form>
<?php
}
elseif (isset($_POST['search']))
{
 
//$_SESSION['s_initiator'] = $initiator;
//$s_initiator = $_SESSION['s_initiator'];
 
//echo $_SESSION['s_initiator']."-"."<br>";
//echo "s_initiator-".$s_initiator."-"."<br>";
 
$substring = "'";
 
$query = "Select * from ci.idea where ";
 
if ($initiator > "")
   {
   $pos=0;
   while (FALSE !== ($pos = strpos($initiator, $substring, $pos))) 
      {
      echo "Cannot have a single quote (') in the "."<b>"."Initiator"."</b>"." text."."<br />";
      echo "You Can Use the Tick Mark (`) If Desired...Correct and Resubmit";
      exit();
      }
   }
 
if ($initiator > "")
   {
   echo $query;
   }
 
}  // End of elseif (isset($_POST['search']))
else
{
}
?> 
<br>&#169;2008 - abcd LLC 
</font>
</body>
</html>

Open in new window

just to be sure, before I go on.
the form is posting to itself. Why would someone hit the back button,

any how.
below is the edited form code that will give you what you want.
oh and this
<form method="post" action="<?php echo $PHP_SELF;?>">
should be
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

also if you want people to be able to use single quotes ' in there text then add

$initiator= str_replace("'", "''", $initiator);
<?php 
session_start();
if(!session_is_registered(myusername))
   {
   header("location:CIIdeasFormMainLogin.php");
   }
 
$signon=$_SESSION['myusername'];
$password=$_SESSION['mypassword'];
$teamname = $_POST["teamname"];
$formdate = $_POST["formdate"];
$ideanumber = $_POST["ideanumber"];
$initiator = $_POST['initiator'];
$mansource = $_POST["mansource"];
$ideamission = $_POST["ideamission"];
$affected = $_POST["affected"];
$impidea = $_POST["impidea"];
$currsit = $_POST["currsit"];
$newidea = $_POST["newidea"];
$factors = $_POST["factors"];
 
//create session variables
if ((!isset($_SESSION['formdate'])) || (!isset($_SESSION['ideanumber'])) || (!isset($_SESSION['initiator'])) || (!isset($_SESSION['mansource'])) || (!isset($_SESSION['ideamission'])) || (!isset($_SESSION['affected'])) || (!isset($_SESSION['impidea'])) || (!isset($_SESSION['currsit'])) || (!isset($_SESSION['newidea'])) || (!isset($_SESSION['factors']))) {
	$_SESSION['formdate'] = $formdate;
	$_SESSION['ideanumber'] = $ideanumber;
	$_SESSION['initiator'] = $initiator;
	$_SESSION['mansource'] = $mansource;
	$_SESSION['ideamission'] = $ideamission;
	$_SESSION['affected'] = $affected;
	$_SESSION['impidea'] = $impidea;
	$_SESSION['currsit'] = $currsit;
	$_SESSION['newidea'] = $newidea;
	$_SESSION['factors'] = $factors;
}
 
if (!isset($_POST['search'])) 
{ // if page is not submitted to itself echo the form
 
?>
<html>
<head><title>Continuous Improvement Ideas Form</title>
<img src="abcd_logo.jpg" align=left hspace=0><img src="abcd_logo.jpg" align=right hspace=0><br><br><P style="font-size: 24pt; color: green" align="center"><b>Continuous Improvement Ideas</b>
<style>
 body
 {
 background: <?php echo '#FFFF99';?>;
 }
</style>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
<hr noshade color=black>
<p style="text-align: center;"><font size=2><b>
COMPLETED FORMS ARE PROCESSED BY THE CONTINUOUS IMPROVEMENT GROUP
</font></b></p>
<hr size=4 noshade color=black>
 
<font size=4 color=red><b> 1) &nbsp;Mission Statement Section</b></font><br>
<font size=2 color=red><i>This section should outline the mission and goal of the idea.</i></font>
 
<table border=2 bordercolor=black >
 <tr>
  <td>
   <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
   <font size=4 color=green><b>Date:&nbsp;
   <font size=4 color=black><b>
   <input type="text" size="11" maxlength="10" name="formdate" value="<?php echo $_SESSION['formdate']; ?>">
 
   &nbsp;</font></b>
   <font size=4 color=green><b>&nbsp;&nbsp;&nbsp;&nbsp;Idea#:&nbsp;</font><font color=black></b>
   <input type="text" size="11" maxlength="10" name="ideanumber" value="<?php echo $_SESSION['ideanumber']; ?>">
   </font><br /><br>
   <font size=4 color=green><b>Inintiator:&nbsp;&nbsp;</font></b>
   <input type="text" size="64" maxlength="64" name="initiator" value="<?php echo $_SESSION['initiator']; ?>"><br>
 
   <br>
   <font size=4 color=green><b>Manufacturing Source:</font></b><br />
   <font color=#990099>Forks I:</font><input type="radio" value="Forks I" name="mansource" <?php if ($_SESSION['mansource'] == "Forks I") { echo "checked=\"checked\""; ?>> &nbsp;&nbsp;&nbsp;&nbsp;
   <font color=#990099>Forks II:<input type="radio" value="Forks II" name="mansource" <?php if ($_SESSION['mansource'] == "Forks II") { echo "checked=\"checked\""; ?>>&nbsp;&nbsp;&nbsp;&nbsp;
   <font color=#990099>LVIP:<input type="radio" value="lvip" name="mansource" <?php if ($_SESSION['mansource'] == "lvip") { echo "checked=\"checked\""; ?>>&nbsp;&nbsp;&nbsp;&nbsp;
   <font color=#990099>Distribution:<input type="radio" value="distribution" name="mansource" <?php if ($_SESSION['mansource'] == "distribution") { echo "checked=\"checked\""; ?>>&nbsp;&nbsp;&nbsp;&nbsp;
   <font color=#990099>Other:<input type="radio" value="other" name="mansource" <?php if ($_SESSION['mansource'] == "other") { echo "checked=\"checked\""; ?>>&nbsp;&nbsp;&nbsp;&nbsp;
   <br />
   <br>
   <font size=4 color=green><b>Idea Mission/Goal:</font></b><br />
   <textarea rows="5" cols="105" name="ideamission" wrap="soft"><?php echo $_SESSION['ideamission']; ?></textarea><br /> 
   <br>
   <font size=4 color=green><b>Product or Line Affected:&nbsp;</font></b><font size=2 color=green><i>
   (What specific product or production line will be affected by the CI event?)</font></i><br />
   <textarea rows="2" cols="105" name="affected" wrap="soft"><?php echo $_SESSION['affected']; ?></textarea><br /> 
   <br>
   <font size=4 color=green><b>Improvement Idea:</font></b><br />
   <textarea rows="5" cols="105" name="impidea" wrap="soft"><?php echo $_SESSION['impidea']; ?></textarea><br /> 
   <br>
  </td>
 </tr>
</table>
 
<hr size=4 noshade color=black><br><br>
 
<font size=4 color=red><b> 2) &nbsp;Current Situation (AS IS)</b></font><br>
<font size=2 color=red><i>Fill out this section to establish a baseline to measure the improvement against.  This should be the "as-is".</i></font>
<p style="text-align: left;"><font size=2><b>Description:</font></b></p>
<textarea rows="7" cols="105" name="currsit" wrap="soft"><?php echo $_SESSION['currsit']; ?></textarea><br /> 
<br><br>
<font size=4 color=red><b> 3) &nbsp;New Ideas (SHOULD BE)</b></font>
<p style="text-align: left;"><font size=2><b>Description:</font></b></p>
<textarea rows="7" cols="105" name="newidea" wrap="soft"><?php echo $_SESSION['newidea']; ?></textarea><br /> 
<br><br>
 
 
   <font size=4 color=red><b> 4) &nbsp;Impacted Factors</b></font><br>
<?php
$conn = OCILogon($signon, $password, 'CIDB') or die("ERROR CONNECTING - Notify Global Support");
$query = "select literal_text from ci.literal where literal_group = 'Impacted Factors' order by seq_no";
$stid = OCI_Parse($conn, $query);
$r = OCI_Execute($stid, OCI_DEFAULT);
if ($r)
   {
   echo "<table border=0>";
   while ($row = OCI_FETCH_ARRAY ($stid, OCI_ASSOC))
      {
      echo "<TR bordercolor=#ffff99>";
      foreach ($row as $item)
         {
         echo "<TD width=858>" . "<font color=#990099>".
         "<input type='checkbox' value = '$item' name = 'factors[]' ".if ($_SESSION['factors'] == $item) { echo "checked=\"checked\"".">" . $item .
         "</font>"."</td>";
         }  // End of FOREACH
      echo "</tr>";
      }  // End of While
   echo "</table>";
   }
else
   {
   echo 'Unable to execute query.  Notify Global Support.';
   }  // end of IF
OCILogoff($conn);
?>
 
<hr size=4 noshade color=black><br>
 
<input type="submit" value="Search" name="search"><br>
<font color=navy> Search for Values Entered in the Form Above.<br></font>
</form>
<?php
}
elseif (isset($_POST['search']))
{
 
//$_SESSION['s_initiator'] = $initiator;
//$s_initiator = $_SESSION['s_initiator'];
 
//echo $_SESSION['s_initiator']."-"."<br>";
//echo "s_initiator-".$s_initiator."-"."<br>";
 
$substring = "'";
 
$query = "Select * from ci.idea where ";
 
if ($initiator > "")
   {
   $pos=0;
   while (FALSE !== ($pos = strpos($initiator, $substring, $pos))) 
      {
      echo "Cannot have a single quote (') in the "."<b>"."Initiator"."</b>"." text."."<br />";
      echo "You Can Use the Tick Mark (`) If Desired...Correct and Resubmit";
      exit();
      }
   }
 
if ($initiator > "")
   {
   echo $query;
   }
 
}  // End of elseif (isset($_POST['search']))
else
{
}
?> 
<br>&#169;2008 - abcd LLC 
</font>
</body>
</html>

Open in new window

Avatar of abrusko

ASKER

nplib,

You are working on another issue with me (Problem with special characters in session variables).  I tried using session variables as a workaround to this problem, that's why I have that other open issue.  I will do what you have suggested here and if it works for me, then hopefully I won't have the other problem with the special characters.

Anyway, to answer your question (Why would someone hit the back button, any how?):

My application is a form where users can enter data in textareas and input areas and I use that information to construct a query to search a database.  If there is an error in some of the data that the user entered in one of the textareas or input areas, I use ECHO to display an error message, but the user must then go back to the form, correct the error and resubmit the query.  

I don't know if ECHO is the right technique to display the error message, but that's all I know how to do for now...this is my very first php app.

Anyway...let me work on this solution and I'll get back to you...thank you very much for your help.

Andy
Avatar of abrusko

ASKER

nplib,

There is an error in the syntax for this line that I am struggling with...can you help?

<input type='checkbox' value = '$item' name = 'factors[]' ".if ($_SESSION['factors'] == $item) { echo "checked=\"checked\"".">" . $item .

Thx,
Andy
Avatar of abrusko

ASKER

Actually, it's part of a bigger ECHO:

echo "<TD width=858>" . "<font color=#990099>".
         "<input type='checkbox' value = '$item' name = 'factors[]' ".if ($_SESSION['factors'] == $item) { echo "checked=\"checked\"".">" . $item .
         "</font>"."</td>";

echo "<TD width=858><font color=#990099><input type='checkbox' value='$item' name = 'factors[]' ";
if ($_SESSION['factors'] == $item) {
     echo "checked=\"checked\"";
}
echo ">$item</font></td>";

Open in new window

Avatar of abrusko

ASKER

Thank you...getting ready to test...
Avatar of abrusko

ASKER

nplib,

Thanks...between this and the other issue I think I just about have this working...thanks very much for your excellent help.

Andy