I'm relatively new to session variables, and have been trying to use them in the following way:

session_start();

$daddr1=$_SESSION["daddr1"];
$daddr2=$_SESSION["daddr2"];
$daddr3=$_SESSION["daddr3"];
$daddr4=$_SESSION["daddr4"];
$daddr5=$_SESSION["daddr5"];

$iaddr1=$_SESSION["iaddr1"];
$iaddr2=$_SESSION["iaddr2"];
$iaddr3=$_SESSION["iaddr3"];
$iaddr4=$_SESSION["iaddr4"];
$iaddr5=$_SESSION["iaddr5"];

$notes=$_SESSION["notes"];
$purch=$_SESSION["purch"];

echo "<form name='form1' action='view_order.php' method='get'>";

echo "<table border='0' width='100%'>";
echo "<tr align='center'>";
  echo "<td width='16%'> </td>"; 
  echo "<td width='16%'>Your purchase Order No.: </td>";
  echo "<td width='16%'><input type='text' name='purch' value=$purch ></td>";
  echo "<td width='16%'> </td>";
  echo "<td width='16%'> </td>";
  echo "<td width='16%'> </td>";
  echo "</tr>";
  
  echo "<tr align='center'>";
  echo "<td width='16%'><br /> </td>"; 
  echo "<td width='16%'></td>";
  echo "<td width='16%'></td>";
  echo "<td width='16%'> </td>";
  echo "<td width='16%'> </td>";
  echo "<td width='16%'> </td>";
  echo "</tr>";
  
echo "<tr align='center'>";
  echo "<td width='16%'>Invoice Address: </td>"; 
  echo "<td width='16%'><input type='text' name='iaddr1' value=$iaddr1 ></td>";
  echo "<td width='16%'>Delivery Address: <br /> (If different to invoice) </td>";
  echo "<td width='16%'><input type='text' name='daddr1' value=$daddr1 ></td>";
  echo "<td width='16%'> </td>";
  echo "<td width='16%'> </td>";
  echo "</tr>";
  echo "<tr align='center'>";
  echo "<td width='16%'> </td>"; 
  echo "<td width='16%'><input type='text' name='iaddr2' value=$iaddr2 ></td>";
  echo "<td width='16%'></td>";
  echo "<td width='16%'><input type='text' name='daddr2' value=$daddr2 > </td>";
  echo "<td width='16%'> </td>";
  echo "<td width='16%'> </td>";
  echo "</tr>";
  echo "<tr align='center'>";
  echo "<td width='16%'> </td>"; 
  echo "<td width='16%'><input type='text' name='iaddr3' value=$iaddr3 ></td>";
  echo "<td width='16%'></td>";
  echo "<td width='16%'> <input type='text' name='daddr3' value=$daddr3 ></td>";
  echo "<td width='16%'> </td>";
  echo "<td width='16%'> </td>";
  echo "</tr>";
  echo "<tr align='center'>";
  echo "<td width='16%'> </td>"; 
  echo "<td width='16%'><input type='text' name='iaddr4' value=$iaddr4 ></td>";
  echo "<td width='16%'></td>";
  echo "<td width='16%'><input type='text' name='daddr4' value=$daddr4 > </td>";
  echo "<td width='16%'> </td>";
  echo "<td width='16%'> </td>";
  echo "</tr>";
  echo "<tr align='center'>";
  echo "<td width='16%'> </td>"; 
  echo "<td width='16%'><input type='text' name='iaddr5' value=$iaddr5 ></td>";
  echo "<td width='16%'></td>";
  echo "<td width='16%'><input type='text' name='daddr5' value=$dadd5 ></td>";
  echo "<td width='16%'></td>";
  echo "<td width='16%'> </td>";
  echo "</tr>";
  echo "</table>";
  echo "<br />";

  echo "<div align=center>Notes: <input type='text' name='notes' size='80' value=$notes ><br />";
  echo "<input type='submit' value='Save' /></form></div>";



$daddr1 = $_GET["daddr1"];
$daddr2 = $_GET["daddr2"];
$daddr3 = $_GET["daddr3"];
$daddr4 = $_GET["daddr4"];
$daddr5 = $_GET["daddr5"];

$iaddr1 = $_GET["iaddr1"];
$iaddr2 = $_GET["iaddr2"];
$iaddr3 = $_GET["iaddr3"];
$iaddr4 = $_GET["iaddr4"];
$iaddr5 = $_GET["iaddr5"];

$notes = $_GET["notes"];
$purch = $_GET["purch"];


$_SESSION["daddr1"] = $daddr1;
$_SESSION["daddr2"] = $daddr2;
$_SESSION["daddr3"] = $daddr3;
$_SESSION["daddr4"] = $daddr4;
$_SESSION["daddr5"] = $daddr5;

$_SESSION["iaddr1"] = $iaddr1;
$_SESSION["iaddr2"] = $iaddr2;
$_SESSION["iaddr3"] = $iaddr3;
$_SESSION["iaddr4"] = $iaddr4;
$_SESSION["iaddr5"] = $iaddr5;
  
$_SESSION["notes"] = $notes;
$_SESSION["purch"] = $purch;

Now this works, in that when I browse to another page, the session variables are all set, however I've noticed some strange behaviour on the form itself.

When you click save for the first time, all the fields disappear, but if save is clicked again, they appear again. Each click after that either populates or empties the text fields.

When you enter the values and press save, I want the text fields to stay populated.

I've tried various if statements around setting the session variables, for example:

if ($iaddr1=="") {
//set first session variables
}

Every if statement I've put in either breaks it completely, or makes no difference.

Any help would be appreciated!

Recommended Answers

All 2 Replies

I think I can see what you are trying to do but just to be sure, why are you defining your $_SESSION variables again at the bottom of the page?

Try dropping the $_GET and $_SESSION variables at the bottom of the page and rewrite the top where you initially set your variables.

if(isset( $_GET["daddr1"]) and $_GET["daddr1"] <> '')
{
$daddr1 = $_GET["daddr1"];
} else { 
$daddr1=$_SESSION["daddr1"];
}

CFROG - You genius!

My question as you pointed out wasn't very clearly presented (I was tired).

I ended up using the exact code you supplied, plus 1 line:

if(isset($_GET["daddr1"]) and $_GET["daddr1"] <> ""){
$daddr1 = $_GET["daddr1"];
$_SESSION["daddr1"] = $daddr1;
}
else {
 $daddr1=$_SESSION["daddr1"];
 }

Works perfectly, and does exactly what I intended it to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.