Hi,
I'm new to PHP/SQL and am having trouble figuring out how to pass form-inputted variables across mulitple pages.
I am writing an application that allows a user to create/modify their companyProfile, add/modify/edit employeeProfile, and print various documents that contain all or some of the data contained in the multiple tables (MySQL database).
This particular piece of code:
- Creates a session for a user who logged into a Web site.
- When the user ADDS an employee to the database, it adds the record to the employeeProfile table in the database (this works). I use auto_increment to create an "EmployeeID" field.
- I ask the user if they want to print the document. If they answer YES, then I want to pass all the newly entered form data to the next page. (I can't get this to work. Employee data is not passed to "f-A_initialnotice.php". Could it be something to do with the HREF tag? Typically, I've been appending the URL with something like "?$last_id" , i.e. <a href="f-A_initialnotice.php?$last_id">YES</a>, but this is not working either.).
Here is the ADD EMPLOYEE code:
<?php
session_start();
if ($_SESSION['valid_log'][0] != $_SERVER['REMOTE_ADDR'] ||
$_SESSION['valid_log'][1] != $_SESSION['User'])
{
//redirect to error page
echo '<META HTTP-EQUIV="Refresh" Content="1; URL=/error_access.html">';
exit();
}
<!-- Begin body -->
/* upon Submit : */
/* If plan type is family/single parent; change Dependents field to YES (default in Db is NO)*/
/* Add record to Db
/* Ask user if they want to print document now? */
/* If YES, pass employee variables to document */
/* If NO, redirect to master employee list */
// If plan type is family or single parent; change Dependents field to YES (default in Db is NO)
if (($HplanType=="SingleParent") || ($HplanType=="Family") || ($DplanType=="SingleParent") ||
($DplanType=="Family"))
{ $Dependents="Yes";
}
print "Dependents: $Dependents<br>";
print "$HplanType<br>";
// update Db
$link = mysql_connect("localhost", "root", "web2004") or die("Could not connect. Please try again later.");
mysql_select_db("ecbp",$link) or die("Could not select database. Please try again later.");
$sql5 = "INSERT INTO employeeProfile
(CompanyID, EmployeeFirst, EmployeeLast, EmployeeStreet1, EmployeeStreet2, EmployeeCity, EmployeeState, EmployeeZip, SpouseFirst, SpouseLast, Dependents, FSA, FSAamount, TAA, HealthPlan, HplanType, DentalPlan, DplanType)
VALUES('$CoID', '$EmployeeFirst', '$EmployeeLast', '$EmployeeStreet1', '$EmployeeStreet2', '$EmployeeCity', '$EmployeeState', '$EmployeeZip', '$SpouseFirst', '$SpouseLast', '$Dependents', '$FSA', '$AmountFSA', '$TAA', '$HealthPlan', '$HplanType', '$DentalPlan', '$DplanType')";
$result5 = mysql_query($sql5);
$EE_id = mysql_insert_id();
$query6 = "SELECT * FROM employeeProfile WHERE EmployeeID = $EE_id";
$result6 = mysql_query($query6) or die('Error with query 6.');
$row6 = mysql_fetch_array($result6);
$_SESSION[$last_id] = $row6;
// display confirmation and ask user if they want to print document
print "<p></p>";
echo '<b><FONT SIZE="4" COLOR="RED">The employee profile information was successfully added.<br><br>';
print "Do you want to Print their Initial COBRA Notice now?</FONT></b><br><br>";
echo '<b><FONT SIZE="4"><a href="f-A_initialnotice.php">YES</a> or <a href="employees.php">NO</a></FONT></b></p>';
/* NEED TO FIX: Employee data is not passed to "f-A_initialnotice.php". Could it be something to do with the HREF tag? Typically, I've been appending the URL with something like "?$last_id" (<a href="f-A_initialnotice.php?$last_id">YES</a> )
but this is not working either.
*/
?>
<!-- END of PHP/MYSQL code -->
Here is the how the "f-A_initialnotice.php" code works:
This confirmation page displays the companyProfile and employeeProfile data in a form, which allows the user to modify if changes are needed. It also asks the user for the date of the letter (this date is not added to the Db, only passed to the document, which is the next page).
When the user hits the "Print" button, I want to pass all the form data to the document. (I can get the company data based on the session, but I'm having trouble figuring out how to pass the employee data, and I haven't even tried the date fields yet.)
Below is the code for "f-A_initialnotice.php"
The company profile data display works, but the employee profile data (that was just added to Db) does not show up.
Any help is appreciated. Thanks!
<!-- PHP session ID code -->
<?php
session_start();
if ($_SESSION['valid_log'][0] != $_SERVER['REMOTE_ADDR'] ||
$_SESSION['valid_log'][1] != $_SESSION['User'])
{
//redirect to error page
echo '<META HTTP-EQUIV="Refresh" Content="1; URL=/error_access.html">';
exit();
}
<!-- Begin "body" -->
<br>
$link = mysql_connect("localhost", "root", "web2004") or die("Could not connect. Please try again later.");
mysql_select_db("ecbp",$link) or die("Could not select database. Please try again later.");
// query the DB to get company profile data
$query = "SELECT * FROM userProfiles,companyProfile WHERE userProfiles.CompanyID=companyProfile.CompanyID";
$result = mysql_query($query) or die("Error with query.");
$row = mysql_fetch_array($result);
//Get Company ID & User ID values
if($row) {
$CoID = $row["CompanyID"];
$id = $row["ProfileID"];
}
/* THIS DOES NOT WORK:
// query the DB to get Employee values
$query2 = "SELECT * FROM employeeProfile WHERE EmployeeID=$EE_id";
$result2 = mysql_query($query) or die("Error with query2.");
$row2 = mysql_fetch_array($result2);
*/
?>
<h2>Review Required Data</h2>
<P>The following displays the information required
for the <B>Initial Notification</B>.<BR>To
make changes, type in the correct information, then click the "Print Form" button.</P>
<P> </P>
<form name="document_data" action="printform/A_initialnotice" method="post">
<!-- insert hidden fields for employee and company ID -->
<input type=hidden name="id" value="<?php echo $last_id ?>"> <input type=hidden name="CoID" value="<?php echo $CoID ?>">
<table border="0" cellspacing="3">
<TR><TD COLSPAN="3"><H2>Company Profile</H2></TD><TR>
<TR><TD ALIGN="right"> Company Name : </TD>
<TD><INPUT TYPE="text" NAME="CompanyName" VALUE="<?php echo $row["CompanyName"] ?>" SIZE="50"></TD>
<TD> </TD></TR>
<TR><TD ALIGN="right">Mailing Address: </TD>
<TD><INPUT TYPE="text" NAME="Street1" VALUE="<?php echo $row["CompanyStreet1"] ?>" SIZE="50"></TD>
<TD CLASS="note">Full street address</TD></TR>
<TR><TD ALIGN="right">City : </TD>
<TD><INPUT TYPE="text" NAME="City" VALUE="<?php echo $row["CompanyCity"] ?>" SIZE="25"></TD>
<TD> </TD></TR>
<TR><TD ALIGN="right">State : </TD>
<TD><INPUT TYPE="text" NAME="State" VALUE="<?php echo $row["CompanyState"] ?>" SIZE="2"></TD>
<TD CLASS="note">2-digit abbreviation</TD></TR>
<TR><TD ALIGN="right">Zip Code : </TD>
<TD><INPUT TYPE="text" NAME="Zip" VALUE="<?php echo $row["CompanyZip"] ?>" SIZE="10"></TD>
<TD CLASS="note">01234 or 01234-5678</TD></TR>
<tr><td COLSPAN="3"><H2>Employee Profile</H2></td></tr>
<TR><TD ALIGN="right">First Name : </TD>
<TD><INPUT TYPE="text" NAME="EmployeeFirst" VALUE="<?php echo $row["EmployeeFirst"] ?>" SIZE="25"></TD>
<TD CLASS="note"> </TD></TR>
<TR><TD ALIGN="right">Last Name : </TD>
<TD><INPUT TYPE="text" NAME="EmployeeLast" VALUE="<?php echo $row["EmployeeLast"] ?>" SIZE="25"></TD>
<TD CLASS="note"> </TD></TR>
<TR><TD ALIGN="right">Street 1 : </TD>
<TD><INPUT TYPE="text" NAME="EmployeeStreet1" VALUE="<?php echo $row["EmployeeStreet1"] ?>" SIZE="25"></TD>
<TD CLASS="note">Full street address</TD></TR>
<TR><TD ALIGN="right">City : </TD>
<TD><INPUT TYPE="text" NAME="EmployeeCity" VALUE="<?php echo $row["EmployeeCity"] ?>" SIZE="25"></TD>
<TD CLASS="note"> </TD></TR><TR>
<TD ALIGN="right">State : </TD>
<TD><SELECT NAME="EmployeeState" SIZE="1">
<OPTION VALUE="<?php echo $row["EmployeeState"] ?>"><?php echo $row["EmployeeState"] ?></OPTION>
</SELECT> </TD>
<TD CLASS="note">2-digit abbreviation</TD></TR>
<TR><TD ALIGN="right">Zip : </TD>
<TD><INPUT TYPE="text" NAME="EmployeeZip" VALUE="<?php echo $row["EmployeeZip"] ?>" SIZE="15"></TD>
<TD CLASS="note">01234 or 01234-5678</TD></TR>
<TR><TD ALIGN="right">Spouse First Name : </TD>
<TD><INPUT TYPE="text" NAME="SpouseFirst" SIZE="25" VALUE="<?php echo $row["SpouseFirst"] ?>"></TD>
<TD CLASS="note"> </TD></TR>
<TR><TD ALIGN="right">Spouse Last Name : </TD>
<TD><INPUT TYPE="text" NAME="SpouseLast" SIZE="25" VALUE="<?php echo $row["SpouseLast"] ?>"></TD>
<TD CLASS="note"> </TD></TR>
<tr><td align="right">Date of Letter : </td>
<td nowrap>
<select name="letter_month" size="1" align="TOP">
<option value="January">January</option>
<!-- rest of option boxes omitted for this post -->
</select>
<select name="letter_day" size="1" align="TOP">
<option value="1">1</option>
<option value="2">2</option>
<!-- rest of option boxes omitted for this post -->
</select>
<select name="letter_year" size="1" align="TOP">
<option value="2004">2004</option>
<option value="2005">2005</option>
</select> </td>
<TD CLASS="note">When will you be mailing the letter?</TD></tr>
<tr><td align="right"> </td><td colspan="2"><input type="submit" name="print" value="Print Form" class="small">
</td></tr>
</table>
<br clear="all">
</form>