Page 1 of 1

Sessions - Can't pass variables

Posted: Sat Jul 31, 2004 12:05 pm
by cdickson
This is a long post, but I'd appreciate any help that you can offer - this is my first time attempting sessions.

I'm having trouble passing information from one form to another.
The order of passing info is:
login.php => order.php => confirmorder.php

The error messages I get on confirmorder.php are (there are lots of them, but they are the same as these, for different variables):
Notice: Undefined index: order_no in /home/michigan/www/www/confirmorder.php on line 56

Notice: Undefined variable: lendname in /home/michigan/www/www/confirmorder.php on line 61
There are 2 database tables:
- Login_users
- Orders

When a customer registers, he/she enters name & company information into the Login-users table on the registration page, and when they place an order the details of that go into the orders table. The database is updating properly. After a customer is registered, they access the area with a login page.

After the customer logs in, they can choose to logout or place an order. When they choose to order, the company information appears at the top of the order.php page (this works so far, but without sessions), and there is a form below for them to fill in the order information.

What I haven't been able to achieve with sessions is passing the company and order information into a confirmation page.

Where do I need to start sessions? At the login page or the order page? Currently I have started them on order.php and confirmorder.php. While I have tried several variations on the code, it currently is as follows:

Code for login.php:

Code: Select all

<?php

$login_real_path = "./";
include($login_real_path . "common.php");

include($login_real_path . 'includes/page_header.php');
$template->getFile(array(
                   'login' => 'login.tpl')
);
$template->addVar("login", array(
                   )
);
$template->parse("login");
include($login_real_path . 'includes/page_footer.php');
?>
Code for order.php:

Code: Select all

<?php session_start();
header("Cache-control: private"); 
?>
<?php require_once('Connections/mfm.php'); ?>
<?php error_reporting(E_ALL); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "orderform")) {
  $insertSQL = sprintf("INSERT INTO orders (order_date, collateral_owner, collateral_address, collateral_city, collateral_state, collateral_zip, collateral_county, collateral_twp, legal_descr, loan_no) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['order_date'], "date"),
                       GetSQLValueString($_POST['collateral_owner'], "text"),
                       GetSQLValueString($_POST['collateral_address'], "text"),
                       GetSQLValueString($_POST['collateral_city'], "text"),
                       GetSQLValueString($_POST['collateral_state'], "text"),
                       GetSQLValueString($_POST['collateral_zip'], "int"),
					   GetSQLValueString($_POST['collateral_county'], "text"),
					   GetSQLValueString($_POST['collateral_twp'], "text"),
                       GetSQLValueString($_POST['legal_descr'], "text"),
                       GetSQLValueString($_POST['loan_no'], "int"));

  mysql_select_db($database_mfm, $mfm);
  $Result1 = mysql_query($insertSQL, $mfm) or die(mysql_error());

  $insertGoTo = "confirmorder.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_mfm, $mfm);
$query_rsUsers = "SELECT * FROM Login_users WHERE username = '$username'";
$rsUsers = mysql_query($query_rsUsers, $mfm) or die(mysql_error());
$row_rsUsers = mysql_fetch_assoc($rsUsers);
$totalRows_rsUsers = mysql_num_rows($rsUsers);

//mysql_select_db($database_mfm, $mfm);
//$query_rsOrders = "SELECT * FROM orders";
//$rsOrders = mysql_query($query_rsOrders, $mfm) or die(mysql_error());
//$row_rsOrders = mysql_fetch_assoc($rsOrders);
//$totalRows_rsOrders = mysql_num_rows($rsOrders);
?>
<html>
<head>
<title>Michigan Flood Maps</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="michmaps.css" rel="stylesheet" type="text/css">
</head>

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" height="100%"  border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td height="50">&nbsp;</td>
    <td height="50"><img src="images/rowone1.gif" width="170" height="50"></td>
    <td height="50"><img src="images/rowone2.gif" width="430" height="50"></td>
    <td height="50"><img src="images/rowone3.gif" width="160" height="50"></td>
    <td height="50">&nbsp;</td>
  </tr>
  <tr>
    <td height="70" align="left">&nbsp;</td>
    <td width="170" align="left"><img src="images/rowtwo1.gif" width="170" height="90"></td>
    <td width="430" align="left"><img src="images/rowtwo2.gif" width="430" height="90"></td>
    <td width="160" align="left"><img src="images/rowtwo3.gif" width="160" height="90"></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td width="170" align="left" valign="top" background="images/leftside_bkgd.gif"><!--#include virtual="menu.html" --></td>
    <td width="430" align="left" valign="top"><p class="heading">Online Order Form </p>
      <p class="navtext">Hello, <b><?php echo $row_rsUsers['f_name']; ?> <?php echo $row_rsUsers['l_name']; ?></b>.&nbsp; We appreciate your business! <br>
        Please fill in your order details below.<br>
        <input name="order_no" type="hidden" id="order_no" value="<?php echo $row_rsOrders['order_no']; ?>">
</p>
      <form action="<?php echo $editFormAction; ?>" name="orderform" method="POST">
        <table width="425" border="0" align="center" cellpadding="1" cellspacing="2">
          <tr class="navtext">
            <td width="200" class="navtext"><b class="heading">Order Date</b></td>
            <td><input name="order_date" type="text" class="navtext" id="order_date">
              <span class="smallbodytext"><br>
              yyyy-mm-dd</span><span class="smallbodytext">              </span></td>
          </tr>
          <tr class="navtext">
            <td colspan="2"><b class="heading">Lender Information</b></td>
          </tr>
          <tr valign="top" class="navtext">
            <td colspan="2" class="navtext"><table width="380"  border="0" align="right" cellpadding="0" cellspacing="0">
              <tr>
                <td class="navtext">
				Lender ID:&nbsp; <?php echo $row_rsUsers['lender_id']; ?><br>                  
				<?php echo $row_rsUsers['lender_name']; ?><br>
                  <?php echo $row_rsUsers['lender_address']; ?><br>
                  <?php echo $row_rsUsers['lender_city']; ?>, 
				  <?php echo $row_rsUsers['lender_state']; ?>&nbsp; 
				  <?php echo $row_rsUsers['lender_zip']; ?><br>
                  Phone:&nbsp; <?php echo $row_rsUsers['lender_phone']; ?><br>
                  Fax:&nbsp; <?php echo $row_rsUsers['lender_fax']; ?><br>
                  Email:&nbsp; <?php echo $row_rsUsers['email']; ?> </td>
              </tr>
            </table></td>
          </tr>
          <tr valign="top" class="navtext">
            <td colspan="2"><hr width="70%" size="1" color="#666666"></td>
          </tr>
          <tr valign="top" class="navtext">
            <td colspan="2"><b class="heading">Property Information</b></td>
          </tr>
          <tr valign="top" class="navtext">
            <td>Loan ID # </td>
            <td><input name="loan_no" type="text" class="navtext" id="loan_no"></td>
          </tr>
          <tr valign="top" class="navtext">
            <td>Owner Name</td>
            <td><input name="collateral_owner" type="text" class="navtext" id="collateral_owner"></td>
          </tr>
          <tr valign="top" class="navtext">
            <td>Property Address </td>
            <td><input name="collateral_address" type="text" class="navtext" id="collateral_address"></td>
          </tr>
          <tr valign="top" class="navtext">
            <td>City</td>
            <td><input name="collateral_city" type="text" class="navtext" id="collateral_city2"></td>
          </tr>
          <tr valign="top" class="navtext">
            <td class="navtext">State</td>
            <td>
              <input name="collateral_state" type="text" class="navtext" id="collateral_state" value="MI" size="10" maxlength="10">
            </td>
          </tr>
          <tr valign="top" class="navtext">
            <td>Zip</td>
            <td><input name="collateral_zip" type="text" class="navtext" id="collateral_zip" size="10" maxlength="10"></td>
          </tr>
          <tr valign="top" class="navtext">
            <td>County</td>
            <td><input name="collateral_county" type="text" class="navtext" id="collateral_county"></td>
          </tr>
          <tr valign="top" class="navtext">
            <td>Township (if applicable)</td>
            <td><input name="collateral_twp" type="text" class="navtext" id="collateral_twp"></td>
          </tr>
          <tr valign="top" class="navtext">
            <td>Legal Description<span class="smallbodytext"> (optional)</span></td>
            <td>&nbsp;</td>
          </tr>
          <tr valign="top" class="navtext">
            <td colspan="2"><textarea name="legal_descr" cols="50" id="legal_descr"></textarea></td>
          </tr>
          <tr class="navtext">
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr class="navtext">
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr class="navtext">
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td colspan="2"><div align="center">
              <input name="Place_Order" type="submit" class="smallbodytext" id="Place_Order" value="Place Order">
            </div></td>
          </tr>
        </table>
        
        <input type="hidden" name="MM_insert" value="orderform">
      </form>      <p class="navtext">&nbsp;</p>
    </td>
    <td width="160" align="center" valign="top" background="images/rightside_bkgd.gif"><table width="100%"  border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td height="60"><img src="images/rightside_topbkgd.gif" width="160" height="60"></td>
      </tr>
      <tr>
        <td><table width="150" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="30">&nbsp;</td>
            <td><div align="center" class="smallbodytext">
              <p class="navtext">&nbsp;</p>
              <p><br>
              </p>
            </div></td>
          </tr>
        </table></td>
      </tr>
    </table></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="40">&nbsp;</td>
    <td width="170"><img src="images/rowthree1.gif" width="170" height="40"></td>
    <td width="430">&nbsp;</td>
    <td width="160" background="images/rightside_bkgd.gif">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="70">&nbsp;</td>
    <td width="170"><img src="images/rowfour1.gif" width="170" height="70"></td>
    <td width="430" align="left" valign="middle" background="images/rowfour2.gif"><table width="430" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="270"><div align="center" class="smallbodytext">Copyright &copy; 2004 Michigan Flood Maps </div></td>
        <td><div align="center"><span class="smallbodytext">Site by <a href="http://www.harborspringswebdesign.com" target="_blank">HSWD </a></span></div></td>
      </tr>
    </table></td>
    <td width="160" background="images/rowfour3.gif">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</body>
</html>
<?php
//mysql_free_result($rsLenders);
mysql_free_result($rsUsers);
//mysql_free_result($rsOrders);
?>
Code for confirmorder.php:

Code: Select all

<?php session_start();
header("Cache-control: private"); 
$_SESSION['lender_name'] = $lendname;
$_SESSION['loan_no'] = $loanno;
?>
<?php require_once('Connections/mfm.php'); ?>
<?php error_reporting(E_ALL); ?>
<?php
mysql_select_db($database_mfm, $mfm);
$query_rsOrders = "SELECT * FROM orders WHERE order_no = '$order_no' ";
$rsOrders = mysql_query($query_rsOrders, $mfm) or die(mysql_error());
$row_rsOrders = mysql_fetch_assoc($rsOrders);
$totalRows_rsOrders = mysql_num_rows($rsOrders);

mysql_select_db($database_mfm, $mfm);
$query_rsLenders = "SELECT * FROM Login_users WHERE userid = 'userid' ";
$rsLenders = mysql_query($query_rsLenders, $mfm) or die(mysql_error());
$row_rsLenders = mysql_fetch_assoc($rsLenders);
$totalRows_rsLenders = mysql_num_rows($rsLenders);
?>
<html>
<head>
<title>Michigan Flood Maps</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="michmaps.css" rel="stylesheet" type="text/css">
</head>

<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" height="100%"  border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td height="50">&nbsp;</td>
    <td height="50"><img src="images/rowone1.gif" width="170" height="50"></td>
    <td height="50"><img src="images/rowone2.gif" width="430" height="50"></td>
    <td height="50"><img src="images/rowone3.gif" width="160" height="50"></td>
    <td height="50">&nbsp;</td>
  </tr>
  <tr>
    <td height="70" align="left">&nbsp;</td>
    <td width="170" align="left"><img src="images/rowtwo1.gif" width="170" height="90"></td>
    <td width="430" align="left"><img src="images/rowtwo2.gif" width="430" height="90"></td>
    <td width="160" align="left"><img src="images/rowtwo3.gif" width="160" height="90"></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td width="170" align="left" valign="top" background="images/leftside_bkgd.gif"><!--#include virtual="menu.html" --></td>
    <td width="430" align="left" valign="top"><p class="heading">Confirming Your  Order</p>
      <form name="confirmorder" method="GET">
        <table width="425" border="0" align="center" cellpadding="1" cellspacing="2">
          <tr class="navtext">
            <td height="25" class="navtext"><b class="heading">Order Date </b></td>
            <td><?php echo date('F d, Y',$_POST['order_date']); ?></td>
          </tr>
          <tr class="navtext">
            <td height="25" class="navtext"><b class="heading">Order Number</b></td>
            <td><?php echo $_POST['order_no'];
			?></td>
          </tr>
          <tr class="navtext">
            <td width="40%" valign="top" class="navtext"><b class="heading">Lender Information</b></td>
            <td><p><?php echo $lendname;?><br>
                <?php echo $row_rsLenders['lender_address']; ?><br>
                <?php echo $row_rsLenders['lender_city']; ?>, <?php echo $row_rsLenders['lender_state']; ?> <?php echo $row_rsLenders['lender_zip']; ?><br>
                <?php echo $row_rsLenders['lender_phone']; ?></p>
            <p>&nbsp;</p></td>
          </tr>
          <tr class="navtext">
            <td valign="top" class="navtext"><b class="heading">Property Information </b></td>
            <td><?php echo $loanno; ?><br>              
			<?php echo $_POST['collateral_owner']; ?><br>
            <?php echo $_POST['collateral_address']; ?><br>
            <?php echo $_POST['collateral_city']; ?>, <?php echo $_POST['collateral_state']; ?>&nbsp; <?php echo $_POST['collateral_zip']; ?><br>
            <?php echo $_POST['county']; ?><br>
            <?php echo $_POST['collateral_twp']; ?><br>
            <?php echo $_POST['legal_descr']; ?></td>
          </tr>
          <tr class="navtext">
            <td class="navtext">&nbsp;</td>
            <td></td>
          </tr>
          <tr class="navtext">
            <td class="navtext">&nbsp;</td>
            <td></td>
          </tr>
          <tr class="navtext">
            <td class="navtext">&nbsp;</td>
            <td></td>
          </tr>
        </table>
      </form>      <p class="navtext">&nbsp;</p>
    </td>
    <td width="160" align="center" valign="top" background="images/rightside_bkgd.gif"><table width="100%"  border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td height="60"><img src="images/rightside_topbkgd.gif" width="160" height="60"></td>
      </tr>
      <tr>
        <td><table width="150" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="30">&nbsp;</td>
            <td><div align="center" class="smallbodytext">
              <p class="navtext">&nbsp;</p>
              <p><br>
              </p>
            </div></td>
          </tr>
        </table></td>
      </tr>
    </table></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="40">&nbsp;</td>
    <td width="170"><img src="images/rowthree1.gif" width="170" height="40"></td>
    <td width="430">&nbsp;</td>
    <td width="160" background="images/rightside_bkgd.gif">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="70">&nbsp;</td>
    <td width="170"><img src="images/rowfour1.gif" width="170" height="70"></td>
    <td width="430" align="left" valign="middle" background="images/rowfour2.gif"><table width="430" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="270"><div align="center" class="smallbodytext">Copyright &copy; 2004 Michigan Flood Maps </div></td>
        <td><div align="center"><span class="smallbodytext">Site by <a href="http://www.harborspringswebdesign.com" target="_blank">HSWD </a></span></div></td>
      </tr>
    </table></td>
    <td width="160" background="images/rowfour3.gif">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($rsOrders);

mysql_free_result($rsLenders);
?>

Posted: Sat Jul 31, 2004 12:14 pm
by kettle_drum
OK, the first "errors" you get are not errors but warnings that variables you are trying to use are not defined, and in the first case the array index is not defined. Looking at your code i cant seem to find anywhere where you have a form item called order_no which is why you get the warning for: echo $_POST['order_no'];

Posted: Sat Jul 31, 2004 12:18 pm
by cdickson
The order number is a field in the "orders" table that is auto-incremented by MySQL. In order.php line 102 the code is:

Code: Select all

<input name="order_no" type="hidden" id="order_no" value="<?php echo $row_rsOrders&#1111;'order_no']; ?>">

Posted: Sat Jul 31, 2004 12:30 pm
by kettle_drum
Ok, well that field is outside of the <form> so it wont be sent - hense the error.

Posted: Sat Jul 31, 2004 12:36 pm
by cdickson
In accordance with a tutorial I have, I tried inserting the following code into the orders.php and the confirmorder.php pages, but I still get error messages.

Code: Select all

<?php session_start();
header("Cache-control: private"); 
$_SESSION['order_no'] = $orderno;
$_SESSION['order_date'] = $_POST['order_date'];
$_SESSION['order_date'] = $orderdate;
$_SESSION['loan_id'] = $_POST['loan_id'];
$_SESSION['loan_id'] = $loanid;
$_SESSION['collateral_owner'] = $_POST['collateral_owner'];
$_SESSION['collateral_owner'] = $owner;
$_SESSION['collateral_address'] = $_POST['collateral_address'];
$_SESSION['collateral_owner'] = $caddress;
$_SESSION['collateral_city'] = $_POST['collateral_city'];
$_SESSION['collateral_city'] = $ccity;
$_SESSION['collateral_state'] = $_POST['collateral_state'];
$_SESSION['collateral_state'] = $cstate;
$_SESSION['collateral_zip'] = $_POST['collateral_zip'];
$_SESSION['collateral_zip'] = $lzip;
$_SESSION['collateral_county'] = $_POST['collateral_county'];
$_SESSION['collateral_county'] = $ccounty;
$_SESSION['collateral_twp'] = $_POST['collateral_twp'];
$_SESSION['collateral_twp'] = $ctwp;
$_SESSION['collateral_legal'] = $_POST['collateral_legal'];
$_SESSION['collateral_legal'] = $clegal;
?>

Posted: Sat Jul 31, 2004 6:03 pm
by litebearer
you are 'doubling up' and therefore wiping out your variables

ie
$_SESSION['order_date'] = $_POST['order_date'];
$_SESSION['order_date'] = $orderdate;
Perhaps a simplistic example will serve to show the flow...


page01.html

Code: Select all

// this is the form page that posts to page02.php

page02.php

Code: Select all

<?PHP
session_start(); 
header("Cache-control: private"); // IE 6 Fix. 

// get your values from the form

$username = trim($_POST['username']);
$userstreet = trim($_POST[''street']);
$userdob    = trim($_POST['birthdate']);
// etc etc

// validate/test the data
if (strlen($username) < 1 || strlen($userstreet) < 1 || strlen($userodb) < 1) {
  // take corrective action
}

// do whatever with the data

// store all the user defined variables into an array

$my_vars = get_defined_vars ( );

// store the array into the session

$_SESSION['my_vars'] = $my_vars;

// proceed to the next page
?>
<META http-equiv="refresh" content="0;URL=page03.php">
<?PHP
?>
page03.php

Code: Select all

<?PHP
session_start(); 
header("Cache-control: private"); // IE 6 Fix. 

// retreive the variables from page02

$my_vars = $_SESSIONS['my_vars'];

$username = $my_vars['username'];
$userstreet = $my_vars['userstreet'];
$userdob = $my_vars['userdob'];
// etc etc

// use the data however you choose
?>

feyd | you know to use

Code: Select all

tags[/color]

Posted: Sat Jul 31, 2004 6:39 pm
by litebearer
oops1 Sorry, old man - poor eyesight - I thought O did but guess I didn't, sorry.

Lite...

Posted: Sun Aug 01, 2004 10:17 am
by cdickson
Thanks for the assist - I will give this a try.