Shoppers Updating their Profiles

 

Introduction.  Now we need to develop the page associated with a shopper updating their profile.  While we intend on having steps within the checkout process where the shopper can access or initiate their profile, these will be developed when we develop the checkout process.  Presently, we are concerned with shoppers that aren't in the check out process.

In our previous pages where we were trying to work with a shopper entering their profile for the first time, we put almost all of the processing within the page profile_new.php.  Now we will separate out the processing into a variety of pages that write to each other to illustrate a different approach and simplify the coding in individual pages at least somewhat.

Overall we need to be able to do several things. 

  • We need to have the update related form on profile_access.php post to a page profile_access_validate.php that validates whether shopper's entries can be found in our profile table
    • if they can be found we need to redirect them to a page, profile_update.php that retrieves their current profile and displays it
    • This update form posts its data to profile_update_process.php which allows the shopper to update it with validation processing similar to what we've done previously
      • if there are errors in the inputs then an error_message is accumulated and the shopper is redirected back to the stick profile_update.php form using a $_SESSION variable, where it is displayed to ease modifications
      • if there are nor errors in the inputs then the inputs are updated in the database and the shopper is routed to profile_updated.php
    • if they can't be found we need to redirect the shopper to a profile_access_failed.php where they are given the possibilities to try again or request that their password be emailed to their address via email_password.php

The following diagram illustrates how these pages interact.

 

 

This approach involves quite a few different PHP pages interacting, posting and sharing information with each other.  Sometimes, this information will be posted from a form to another page, other times we will make sue of session variables to make sure the information is available when it is needed.  Remember we have
  • variables/information local to a function/method
  • variables/information local to a page
  • variables/information posted from a form
  • session variables

It is important to keep your eyes open about what is defined, updatable and accessible where.

So this webpage will be quite large because we have so many pages to develop.

This next table contains a set of code for where the shopper is routed if their email and password aren't found in our profile table.  You need to call this page profile_access_failed.php.

 

<html>
<!-- profile_access_failed.php - accessing a profile -->
<head>
<title>Accessing a Profile</title>
</head>
<?php
include("includes/header.php");
?>

<br><p align="center"><font size=5 color="#FF0000"><b>The email and password you entered<br>
were not found in our database.</b></font></p>
<div align="center">
<table border="0" cellpadding="3" cellspacing="0" align="center" width="400" id="layout_table">
<tr>
<td width="400" align="center"><b><font size="4" color="#999966">Access an
Existing Profile<br></font></b></td>
</tr>
<tr>
<td width="400">
<form method="POST" action="profile_access_validate.php">
<div align="center">
<table border="0" cellpadding="5" cellspacing="0" width="400" id="table_access">
<tr>
<td width="175" align=right><b><font size="4">E-Mail:</font></b></td>
<td width="225">
<input type="text" name="txt_original_email" size="30" style="font-size: 14pt"></td>
</tr>
<tr>
<td width="175" align="right"><b><font size="4">Password:</font></b></td>
<td width="225">
<input type="password" name="txt_original_password" size="12" style="font-size: 14pt"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit" name="cmd_access_rpofile" style="font-size: 14pt; font-weight: bold"></td>
</tr>
</table>
</div>
</form>
</td>
</tr>
<tr>
<td align="center"><font color="#999966"><b>Forgotten your password,
<a href="email_password.php">click here</a>?</b></font></td>
</tr>
</table>
</div>
</body>
</html>

 

This page will look like the following and give the shopper the option of trying again or just having firefox email them their password to the email address they enter.

 

 

Now we need to do the processing that validates whether the shopper has entered information we have in our database.  This will be done in the following processing validation page, that only redirects shoppers to other form pages based on what is found in the database.

You should call this page profile_access_validate.php.

 

<html>
<?php
include("includes/connection_config.php");
?>
<body>
<?php
session_start();
$_SESSION["email_original"] = $txt_original_email;
$_SESSION["password_original"] = $txt_original_password;
// need to connect to the current data to make sure
// that the email address hasn't already been used

// connecting to the database on battcave.com
$link = mysql_connect($host, $user, $password);
// creating the query string
$query_string = "SELECT * FROM profile WHERE (email = '$txt_original_email' AND password = '$txt_original_password')";
// executing the SQL statement
$result_set = mysql_db_query($db_name, $query_string, $link);
// if the resultset has any entries then you need to let the Shopper
// know the email address is already in use

if (mysql_fetch_array($result_set))
{
mysql_close($link);
header("location:profile_update.php");
}
else
{
mysql_close($link);
header("location:profile_access_failed.php");
}
?>
</body>
</html>

 

Notice how this redirects the shopper to either profile_access_failed.php or profile_update.php depending on whether the shopper's entries are found.

But, the major and most complicated task for this webpage is to update an existing profile.

The next table contains the code for profile_update.php.  This is primarily a page that retrieves a valid shopper's profile info so that the shopper can modify it and update it.  The validation and processing is done on a separate page.  Even though it might result in word wrap, I try to make sure to represent the control/looping/decision structures using indenting.

 

<html>
<!-- profile_update.php - the form page for updating a profile -->
<head><title>Updating Your Profile at Firefox</title></head>
<?php
include("includes/header.php");
include("includes/connection_config.php");
// creating the variables to hold the email and password
// entered for validation

session_start();
$txt_original_email = $_SESSION["email_original"];
$txt_original_password = $_SESSION["password_original"];
// if an error message was developed in the processing page it is displayed
if ($_SESSION["error_message"] != "")
{

echo "<center><font size=4 color=ff0000>";
echo $_SESSION["error_message"];
echo "</center></font>";

}
// need to connect to the current data to obtain
// all the data on this shopper

// connecting to the database on battcave.com
$link = mysql_connect($host, $user, $password);
// creating the query string
$query_string = "SELECT * FROM profile WHERE (email = '$txt_original_email' AND password = '$txt_original_password')";
// executing the SQL statement
$result_set = mysql_db_query($db_name, $query_string, $link);
// the $result_set can only contain one row because of all our validation and error trapping

while ($row = mysql_fetch_array($result_set))
{

// assigning the database values into local variables
// to use in filling in the form

$txt_email = $row[email];
$txt_password = $row[password];
$txt_bill_first_name = $row[bill_first_name];
$txt_bill_last_name = $row[bill_last_name];
$txt_bill_address = $row[bill_address];
$txt_bill_city = $row[bill_city];
$sel_bill_state = $row[bill_state];
$txt_bill_zip = $row[bill_zipcode];
$txt_bill_phone = $row[bill_phone];
$txt_bill_fax = $row[bill_fax];
$txt_ship_first_name = $row[ship_first_name];
$txt_ship_last_name = $row[ship_last_name];
$txt_ship_address = $row[ship_address];
$txt_ship_city = $row[ship_city];
$sel_ship_state = $row[ship_state];
$txt_ship_zip = $row[ship_zipcode];
$txt_ship_phone = $row[ship_phone];
$txt_ship_fax = $row[ship_fax];
$sel_card_type = $row[card_type];
$txt_card_number = $row[card_number];
$sel_card_month = $row[expiration_month];
$sel_card_year = $row[expiration_year];
$txt_card_name = $row[card_name];
$display_date_registered = $row[date_registered];

}

?>

<br><br>
<div align="center">
<form method="POST" action="profile_update_process.php">
<table border="0" cellpadding="6" cellspacing="0" width="600" id="table1">
<tr>
<td colspan = 2 align=center><b><font size="4">*</font> denotes a required entry</b></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">*E-Mail Address:</font></td>
<td width="387">
<input type="text" name="txt_email" size="30" style="font-size: 14pt" value = "<?php if (isset($txt_email)) echo $txt_email; ?>"></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">*Password:</font></td>
<td width="387">
<input type="password" name="txt_password" size="12" style="font-size: 14pt" value = "<?php if (isset($txt_password)) echo $txt_password; ?>"></td>
</tr>
<tr>
<td width="189" align="right">&nbsp;</td>
<td width="387">&nbsp;</td>
</tr>
<tr>
<td colspan="2" align="center"><b>
<font size="5" color="#A36436">Billing Information</font></b></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">*Name:</font></td>
<td width="387">
<input name="txt_bill_first_name" size="15" style="font-size: 14pt" value = "<?php if (isset($txt_bill_first_name)) echo $txt_bill_first_name; ?>">
<input name="txt_bill_last_name" size="20" style="font-size: 14pt" value = "<?php if (isset($txt_bill_last_name)) echo $txt_bill_last_name; ?>">
</td>
</tr>
<tr>
<td width="189" align="right"><font size="4">*Street Address:</font></td>
<td width="387">
<input name="txt_bill_address" size="40" style="font-size: 14pt" value = "<?php if (isset($txt_bill_address)) echo $txt_bill_address; ?>"></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">*City:</font></td>
<td width="387">
<input name="txt_bill_city" size="20" style="font-size: 14pt" value = "<?php if (isset($txt_bill_city)) echo $txt_bill_city; ?>"></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">*State:</font></td>
<td width="387">
<select size="1" name="sel_bill_state" style="font-size: 14pt">
<option value = "no_select" <?php if ($sel_bill_state == 'no_select') echo 'selected'; ?>>Please Select</option>
<option value = "Alabama" <?php if ($sel_bill_state == 'Alabama') echo 'selected'; ?>>Alabama</option>
<option value = "Alaska" <?php if ($sel_bill_state == 'Alaska') echo 'selected'; ?>>Alaska</option>
<option value = "Arizona" <?php if ($sel_bill_state == 'Arizona') echo 'selected'; ?>>Arizona</option>
<option value = "Arkansas" <?php if ($sel_bill_state == 'Arkansas') echo 'selected'; ?>>Arkansas</option>
<option value = "California" <?php if ($sel_bill_state == 'California') echo 'selected'; ?>>California</option>
<option value = "Colorado" <?php if ($sel_bill_state == 'Colorado') echo 'selected'; ?>>Colorado</option>
<option value = "Connecticut" <?php if ($sel_bill_state == 'Connecticut') echo 'selected'; ?>>Connecticut</option>
<option value = "Delaware" <?php if ($sel_bill_state == 'Delaware') echo 'selected'; ?>>Delaware</option>
<option value = "Florida" <?php if ($sel_bill_state == 'Florida') echo 'selected'; ?>>Florida</option>
<option value = "Georgia" <?php if ($sel_bill_state == 'Georgia') echo 'selected'; ?>>Georgia</option>
<option value = "Hawaii" <?php if ($sel_bill_state == 'Hawaii') echo 'selected'; ?>>Hawaii</option>
<option value = "Idaho" <?php if ($sel_bill_state == 'Idaho') echo 'selected'; ?>>Idaho</option>
<option value = "Illinois" <?php if ($sel_bill_state == 'Illinois') echo 'selected'; ?>>Illinois</option>
<option value = "Indiana" <?php if ($sel_bill_state == 'Indiana') echo 'selected'; ?>>Indiana</option>
<option value = "Iowa" <?php if ($sel_bill_state == 'Iowa') echo 'selected'; ?>>Iowa</option>
<option value = "Kansas" <?php if ($sel_bill_state == 'Kansas') echo 'selected'; ?>>Kansas</option>
<option value = "Kentucky" <?php if ($sel_bill_state == 'Kentucky') echo 'selected'; ?>>Kentucky</option>
<option value = "Louisiana" <?php if ($sel_bill_state == 'Louisiana') echo 'selected'; ?>>Lousiiana</option>
<option value = "Maine" <?php if ($sel_bill_state == 'Maine') echo 'selected'; ?>>Maine</option>
<option value = "Maryland" <?php if ($sel_bill_state == 'Maryland') echo 'selected'; ?>>Maryland</option>
<option value = "Massachusetts" <?php if ($sel_bill_state == 'Massachusetts') echo 'selected'; ?>>Massachusetts</option>
<option value = "Michigan" <?php if ($sel_bill_state == 'Michigan') echo 'selected'; ?>>Michigan</option>
<option value = "Minnesota" <?php if ($sel_bill_state == 'Minnesota') echo 'selected'; ?>>Minnesota</option>
<option value = "Mississippi" <?php if ($sel_bill_state == 'Mississippi') echo 'selected'; ?>>Mississippi</option>
<option value = "Missouri" <?php if ($sel_bill_state == 'Missouri') echo 'selected'; ?>>Missouri</option>
<option value = "Montana" <?php if ($sel_bill_state == 'Montana') echo 'selected'; ?>>Montana</option>
<option value = "Nebraska" <?php if ($sel_bill_state == 'Nebraska') echo 'selected'; ?>>Nebraska</option>
<option value = "Nevada" <?php if ($sel_bill_state == 'Nevada') echo 'selected'; ?>>Nevada</option>
<option value = "NewHampshire" <?php if ($sel_bill_state == 'NewHampshire') echo 'selected'; ?>>New Hampshire</option>
<option value = "NewJersey" <?php if ($sel_bill_state == 'NewJersey') echo 'selected'; ?>>New Jersey</option>
<option value = "NewMexico" <?php if ($sel_bill_state == 'NewMexico') echo 'selected'; ?>>New Mexico</option>
<option value = "NewYork" <?php if ($sel_bill_state == 'NewYork') echo 'selected'; ?>>New York</option>
<option value = "NorthCarolina" <?php if ($sel_bill_state == 'NorthCarolina') echo 'selected'; ?>>North Carolina</option>
<option value = "NorthDakota" <?php if ($sel_bill_state == 'NorthDakota') echo 'selected'; ?>>North Dakota</option>
<option value = "Ohio" <?php if ($sel_bill_state == 'Ohio') echo 'selected'; ?>>Ohio</option>
<option value = "Oklahoma" <?php if ($sel_bill_state == 'Oklahoma') echo 'selected'; ?>>Oklahoma</option>
<option value = "Oregon" <?php if ($sel_bill_state == 'Oregon') echo 'selected'; ?>>Oregon</option>
<option value = "Pennsylvania" <?php if ($sel_bill_state == 'Pennsylvania') echo 'selected'; ?>>Pennsylvania</option>
<option value = "RhodeIsland" <?php if ($sel_bill_state == 'RhodeIsland') echo 'selected'; ?>>Rhode Island</option>
<option value = "SouthCarolina" <?php if ($sel_bill_state == 'SouthCarolina') echo 'selected'; ?>>South Carolina</option>
<option value = "SouthDakota" <?php if ($sel_bill_state == 'SouthDakota') echo 'selected'; ?>>South Dakota</option>
<option value = "Tennessee" <?php if ($sel_bill_state == 'Tennessee') echo 'selected'; ?>>Tennessee</option>
<option value = "Texas" <?php if ($sel_bill_state == 'Texas') echo 'selected'; ?>>Texas</option>
<option value = "Utah" <?php if ($sel_bill_state == 'Utah') echo 'selected'; ?>>Utah</option>
<option value = "Vermont" <?php if ($sel_bill_state == 'Vermont') echo 'selected'; ?>>Vermont</option>
<option value = "Virginia" <?php if ($sel_bill_state == 'Virginia') echo 'selected'; ?>>Virginia</option>
<option value = "Washington" <?php if ($sel_bill_state == 'Washington') echo 'selected'; ?>>Washington</option>
<option value = "WestVirginia" <?php if ($sel_bill_state == 'WestVirginia') echo 'selected'; ?>>West Virginia</option>
<option value = "Wisconsin" <?php if ($sel_bill_state == 'Wisconsin') echo 'selected'; ?>>Wisconsin</option>
<option value = "Wyoming" <?php if ($sel_bill_state == 'Wyoming') echo 'selected'; ?>>Wyoming</option>
</select></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">*Zip:</font></td>
<td width="387">
<input name="txt_bill_zip" size="10" style="font-size: 14pt" value = "<?php if (isset($txt_bill_zip)) echo $txt_bill_zip; ?>"></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">Billing Phone:</font></td>
<td width="387">
<input name="txt_bill_phone" size="12" style="font-size: 14pt" value = "<?php if (isset($txt_bill_phone)) echo $txt_bill_phone; ?>"></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">Billing Fax:</font></td>
<td width="387">
<input name="txt_bill_fax" size="12" style="font-size: 14pt" value = "<?php if (isset($txt_bill_fax)) echo $txt_bill_fax; ?>"></td>
</tr>
<tr>
<td width="189" align="right">&nbsp;</td>
<td width="387">&nbsp;</td>
</tr>
<tr>
<td align="center" colspan="2"><font size="5" color="#A36436">
<b>Shipping Information</font></b></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">Same as Billing:</font></td>
<td width="387">
<input type="checkbox" name="chk_same_as" value="repeat" style="font-size: 14pt" <?php if ($chk_same_as == 'repeat' ) echo 'checked'; ?>></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">*Name:</font></td>
<td width="387">
<input name="txt_ship_first_name" size="15" style="font-size: 14pt" value = "<?php if (isset($txt_ship_first_name)) echo $txt_ship_first_name; ?>">
<input name="txt_ship_last_name" size="20" style="font-size: 14pt" value = "<?php if (isset($txt_ship_last_name)) echo $txt_ship_last_name; ?>"></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">*Street Address:</font></td>
<td width="387">
<input name="txt_ship_address" size="40" style="font-size: 14pt" value = "<?php if (isset($txt_ship_address)) echo $txt_ship_address; ?>"></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">*City:</font></td>
<td width="387">
<input name="txt_ship_city" size="20" style="font-size: 14pt" value = "<?php if (isset($txt_ship_city)) echo $txt_ship_city; ?>"></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">*State:</font></td>
<td width="387">
<select size="1" name="sel_ship_state" style="font-size: 14pt">
<option value = "no_select" <?php if (($sel_ship_state) == 'no_select') echo 'selected'; ?>>Please Select</option>
<option value = "Alabama" <?php if (($sel_ship_state) == 'Alabama') echo 'selected'; ?>>Alabama</option>
<option value = "Alaska" <?php if (($sel_ship_state) == 'Alaska') echo 'selected'; ?>>Alaska</option>
<option value = "Arizona" <?php if (($sel_ship_state) == 'Arizona') echo 'selected'; ?>>Arizona</option>
<option value = "Arkansas" <?php if (($sel_ship_state) == 'Arkansas') echo 'selected'; ?>>Arkansas</option>
<option value = "California" <?php if (($sel_ship_state) == 'California') echo 'selected'; ?>>California</option>
<option value = "Colorado" <?php if (($sel_ship_state) == 'Colorado') echo 'selected'; ?>>Colorado</option>
<option value = "Connecticut" <?php if (($sel_ship_state) == 'Connecticut') echo 'selected'; ?>>Connecticut</option>
<option value = "Delaware" <?php if (($sel_ship_state) == 'Delaware') echo 'selected'; ?>>Delaware</option>
<option value = "Florida" <?php if (($sel_ship_state) == 'Florida') echo 'selected'; ?>>Florida</option>
<option value = "Georgia" <?php if (($sel_ship_state) == 'Georgia') echo 'selected'; ?>>Georgia</option>
<option value = "Hawaii" <?php if (($sel_ship_state) == 'Hawaii') echo 'selected'; ?>>Hawaii</option>
<option value = "Idaho" <?php if (($sel_ship_state) == 'Idaho') echo 'selected'; ?>>Idaho</option>
<option value = "Illinois" <?php if (($sel_ship_state) == 'Illinois') echo 'selected'; ?>>Illinois</option>
<option value = "Indiana" <?php if (($sel_ship_state) == 'Indiana') echo 'selected'; ?>>Indiana</option>
<option value = "Iowa" <?php if (($sel_ship_state) == 'Iowa') echo 'selected'; ?>>Iowa</option>
<option value = "Kansas" <?php if (($sel_ship_state) == 'Kansas') echo 'selected'; ?>>Kansas</option>
<option value = "Kentucky" <?php if (($sel_ship_state) == 'Kentucky') echo 'selected'; ?>>Kentucky</option>
<option value = "Louisiana" <?php if (($sel_ship_state) == 'Louisiana') echo 'selected'; ?>>Lousiiana</option>
<option value = "Maine" <?php if (($sel_ship_state) == 'Maine') echo 'selected'; ?>>Maine</option>
<option value = "Maryland" <?php if (($sel_ship_state) == 'Maryland') echo 'selected'; ?>>Maryland</option>
<option value = "Massachusetts" <?php if (($sel_ship_state) == 'Massachusetts') echo 'selected'; ?>>Massachusetts</option>
<option value = "Michigan" <?php if (($sel_ship_state) == 'Michigan') echo 'selected'; ?>>Michigan</option>
<option value = "Minnesota" <?php if (($sel_ship_state) == 'Minnesota') echo 'selected'; ?>>Minnesota</option>
<option value = "Mississippi" <?php if (($sel_ship_state) == 'Mississippi') echo 'selected'; ?>>Mississippi</option>
<option value = "Missouri" <?php if (($sel_ship_state) == 'Missouri') echo 'selected'; ?>>Missouri</option>
<option value = "Montana" <?php if (($sel_ship_state) == 'Montana') echo 'selected'; ?>>Montana</option>
<option value = "Nebraska" <?php if (($sel_ship_state) == 'Nebraska') echo 'selected'; ?>>Nebraska</option>
<option value = "Nevada" <?php if (($sel_ship_state) == 'Nevada') echo 'selected'; ?>>Nevada</option>
<option value = "NewHampshire" <?php if (($sel_ship_state) == 'NewHampshire') echo 'selected'; ?>>New Hampshire</option>
<option value = "NewJersey" <?php if (($sel_ship_state) == 'NewJersey') echo 'selected'; ?>>New Jersey</option>
<option value = "NewMexico" <?php if (($sel_ship_state) == 'NewMexico') echo 'selected'; ?>>New Mexico</option>
<option value = "NewYork" <?php if (($sel_ship_state) == 'NewYork') echo 'selected'; ?>>New York</option>
<option value = "NorthCarolina" <?php if (($sel_ship_state) == 'NorthCarolina') echo 'selected'; ?>>North Carolina</option>
<option value = "NorthDakota" <?php if (($sel_ship_state) == 'NorthDakota') echo 'selected'; ?>>North Dakota</option>
<option value = "Ohio" <?php if (($sel_ship_state) == 'Ohio') echo 'selected'; ?>>Ohio</option>
<option value = "Oklahoma" <?php if (($sel_ship_state) == 'Oklahoma') echo 'selected'; ?>>Oklahoma</option>
<option value = "Oregon" <?php if (($sel_ship_state) == 'Oregon') echo 'selected'; ?>>Oregon</option>
<option value = "Pennsylvania" <?php if (($sel_ship_state) == 'Pennsylvania') echo 'selected'; ?>>Pennsylvania</option>
<option value = "RhodeIsland" <?php if (($sel_ship_state) == 'RhodeIsland') echo 'selected'; ?>>Rhode Island</option>
<option value = "SouthCarolina" <?php if (($sel_ship_state) == 'SouthCarolina') echo 'selected'; ?>>South Carolina</option>
<option value = "SouthDakota" <?php if (($sel_ship_state) == 'SouthDakota') echo 'selected'; ?>>South Dakota</option>
<option value = "Tennessee" <?php if (($sel_ship_state) == 'Tennessee') echo 'selected'; ?>>Tennessee</option>
<option value = "Texas" <?php if (($sel_ship_state) == 'Texas') echo 'selected'; ?>>Texas</option>
<option value = "Utah" <?php if (($sel_ship_state) == 'Utah') echo 'selected'; ?>>Utah</option>
<option value = "Vermont" <?php if (($sel_ship_state) == 'Vermont') echo 'selected'; ?>>Vermont</option>
<option value = "Virginia" <?php if (($sel_ship_state) == 'Virginia') echo 'selected'; ?>>Virginia</option>
<option value = "Washington" <?php if (($sel_ship_state) == 'Washington') echo 'selected'; ?>>Washington</option>
<option value = "WestVirginia" <?php if (($sel_ship_state) == 'WestVirginia') echo 'selected'; ?>>West Virginia</option>
<option value = "Wisconsin" <?php if (($sel_ship_state) == 'Wisconsin') echo 'selected'; ?>>Wisconsin</option>
<option value = "Wyoming" <?php if (($sel_ship_state) == 'Wyoming') echo 'selected'; ?>>Wyoming</option>
</select></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">*Zip:</font></td>
<td width="387">
<input name="txt_ship_zip" size="10" style="font-size: 14pt" value = "<?php if (isset($txt_ship_zip)) echo $txt_ship_zip; ?>"></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">Shipping Phone:</font></td>
<td width="387">
<input name="txt_ship_phone" size="12" style="font-size: 14pt" value = "<?php if (isset($txt_ship_phone)) echo $txt_ship_phone; ?>"></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">Shipping Fax:</font></td>
<td width="387">
<input name="txt_ship_fax" size="12" style="font-size: 14pt" value = "<?php if (isset($txt_ship_fax)) echo $txt_ship_fax; ?>"></td>
</tr>
<tr>
<td width="189" align="right">&nbsp;</td>
<td width="387">&nbsp;</td>
</tr>
<tr>
<td width="576" align="center" colspan="2">
<font size="5" color="#A36436"><b>Credit Card Information</font></b></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">*Name on Card:</font></td>
<td width="387">
<input name="txt_card_name" size="40" style="font-size: 14pt" value = "<?php if (isset($txt_card_name)) echo $txt_card_name; ?>">
</td>
</tr>
<tr>
<td width="189" align="right"><font size="4">*Card Type:</font></td>
<td width="387">
<select size="1" name="sel_card_type" style="font-size: 14pt">
<option value = "no_select" <?php if (($sel_card_type) == 'no_select') echo 'selected'; ?>>Please Select</option>
<option value = "AmEx" <?php if (($sel_card_type) == 'AmEx') echo 'selected'; ?>>American Express</option>
<option value = "Discover" <?php if (($sel_card_type) == 'Discover') echo 'selected'; ?>>Discover</option>
<option value = "Mastercard" <?php if (($sel_card_type) == 'Mastercard') echo 'selected'; ?>>Mastercard</option>
<option value = "Visa" <?php if (($sel_card_type) == 'Visa') echo 'selected'; ?>>Visa</option>
</select></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">*Card Number:</font></td>
<td width="387">
<input name="txt_card_number" size="20" style="font-size: 14pt" value = "<?php if (isset($txt_card_number)) echo $txt_card_number; ?>"></td>
</tr>
<tr>
<td width="189" align="right"><font size="4">*Expiration Date:</font></td>
<td width="387">
<select size="1" name="sel_card_month" style="font-size: 14pt">
<option value = "January" <?php if (($sel_card_month) == 'January') echo 'selected'; ?>>January</option>
<option value = "February" <?php if (($sel_card_month) == 'February') echo 'selected'; ?>>February</option>
<option value = "March" <?php if (($sel_card_month) == 'March') echo 'selected'; ?>>March</option>
<option value = "April" <?php if (($sel_card_month) == 'April') echo 'selected'; ?>>April</option>
<option value = "May" <?php if (($sel_card_month) == 'May') echo 'selected'; ?>>May</option>
<option value = "June" <?php if (($sel_card_month) == 'June') echo 'selected'; ?>>June</option>
<option value = "July" <?php if (($sel_card_month) == 'July') echo 'selected'; ?>>July</option>
<option value = "August" <?php if (($sel_card_month) == 'August') echo 'selected'; ?>>August</option>
<option value = "September" <?php if (($sel_card_month) == 'September') echo 'selected'; ?>>September</option>
<option value = "October" <?php if (($sel_card_month) == 'October') echo 'selected'; ?>>October</option>
<option value = "November" <?php if (($sel_card_month) == 'November') echo 'selected'; ?>>November</option>
<option value = "December" <?php if (($sel_card_month) == 'December') echo 'selected'; ?>>December</option>
</select>
<select size="1" name="sel_card_year" style="font-size: 14pt">
<option value = "2004" <?php if (($sel_card_year) == '2004') echo 'selected'; ?>>2004</option>
<option value = "2005" <?php if (($sel_card_year) == '2005') echo 'selected'; ?>>2005</option>
<option value = "2006" <?php if (($sel_card_year) == '2006') echo 'selected'; ?>>2006</option>
<option value = "2007" <?php if (($sel_card_year) == '2007') echo 'selected'; ?>>2007</option>
<option value = "2008" <?php if (($sel_card_year) == '2008') echo 'selected'; ?>>2008</option>
<option value = "2009" <?php if (($sel_card_year) == '2009') echo 'selected'; ?>>2009</option>
<option value = "2010" <?php if (($sel_card_year) == '2010') echo 'selected'; ?>>2010</option>
</select></td>
</tr>
<tr>
<td width="189" align="right">&nbsp;</td>
<td width="387">&nbsp;</td>
</tr>
<tr>
<td width="576" align="center" colspan="2">
<input type="submit" value="Submit" name="submit_new_profile" style="font-size: 14pt; font-weight: bold"></td>
</tr>
</table>
<p>&nbsp;</p>
</form>
</div>

</body>
</html>

 

 

Now, we attempt to describe the code.

Generally, the page can be broken down into a section involving most of executable PHP code.  The lower portion relates primarily to the form itself and making the form sticky.  We will focus on the PHP code that does most of the processing for this writing.  But, we will discuss the sticky form in class.

  • Initially we establish the page and include the header.php
  • We then make sure we have a local variable that contains session variables for the original email and password the shopper has entered
  • Then we have a small snippet that checks to see if a session variable containing and error message has any content
    • if it does then it is echoed on this form in order to help the user complete the form properly
  • Then we connect to the database
  • We create a query that will search for the email and password supplied by the user
    • if such a user is found
      • we set local variables that can be used to echo the values found in the record into the form

Everything else relates to sticky form processing which we will leave for class, particularly since we have discussed these issues in earlier webpages.

Now we need to develop the PHP related to processing this form and either accumulating the error message to feedback to the user or updating the info they've provided in the database.  Though there are other fairly important components relating to verifying the email address isn't being used by someone else and providing session variables for use elsewhere.  But you should call this page profile_update_process.php.

 

<html>
<!-- profile_update_process.php - the processing page for updating a profile -->
<?php
include("includes/connection_config.php");
?>
<body>
<?php
// creating the variables to hold the email and password
// entered for validation

session_start();
$txt_original_email = $_SESSION["email_original"];
$txt_original_password = $_SESSION["password_original"];

// initializing a blank error message
// this will be used to accumulate the input errors
// in order to feed them back to the user

$error_message = "";
// if it has been submitted
// each of the form entries will be examined
// to see which have been omitted

if ($txt_email == "")
{

$error_message = "$error_message<br>You have omitted your email address.";

}
else
{

// need to connect to the current data to make sure
// that the email address hasn't already been used

// connecting to the database on battcave.com
$link = mysql_connect($host, $user, $password);
// creating the query string
$query_string = "SELECT * FROM profile WHERE (email = '$txt_email')";
// executing the SQL statement
$result_set = mysql_db_query($db_name, $query_string, $link);
// if the resultset has any entries then you need to let the Shopper
// know the email address is already in use

while ($row = mysql_fetch_array($result_set))
{

if ($txt_email != $txt_original_email)
{

$error_message = "$error_message<br><br>This email address is already in use.<br>";

}

}

mysql_close($link);

}
if ($txt_password == "")
{

$error_message = "$error_message<br>You have omitted your password.";

}
if ($txt_bill_first_name == "")
{

$error_message = "$error_message<br>You have omitted your billing first name.";

}
if ($txt_bill_last_name == "")
{

$error_message = "$error_message<br>You have omitted your billing last name.";

}
if ($txt_bill_address == "")
{

$error_message = "$error_message<br>You have omitted your billing address.";

}
if ($txt_bill_city == "")
{

$error_message = "$error_message<br>You have omitted your billing city.";

}
if ($sel_bill_state == "no_select")
{

$error_message = "$error_message<br>You have omitted your billing state.";

}
if ($txt_bill_zip == "")
{

$error_message = "$error_message<br>You have omitted your billing zipcode.";

}
// this section checks for entries in the credit card information
if ($txt_card_name == "")
{

$error_message = "$error_message<br>You have omitted the name on your credit card.";

}
if ($sel_card_type == "no_select")
{

$error_message = "$error_message<br>You have omitted the your credit card type.";

}
if ($txt_card_number == "")
{

$error_message = "$error_message<br>You have omitted your credit card number.";

}
// this section of code makes sure the expiration date
// is after today's date based on comparing the month and year

$month_number = date('m');
$year_number = date('Y');
// converting month names to numbers for comparison
if ($sel_card_month == "January") $sel_month_number = 1;
if ($sel_card_month == "February") $sel_month_number = 2;
if ($sel_card_month == "March") $sel_month_number = 3;
if ($sel_card_month == "April") $sel_month_number = 4;
if ($sel_card_month == "May") $sel_month_number = 5;
if ($sel_card_month == "June") $sel_month_number = 6;
if ($sel_card_month == "July") $sel_month_number = 7;
if ($sel_card_month == "August") $sel_month_number = 8;
if ($sel_card_month == "September") $sel_month_number = 9;
if ($sel_card_month == "October") $sel_month_number = 10;
if ($sel_card_month == "November") $sel_month_number = 11;
if ($sel_card_month == "December") $sel_month_number = 12;
// testing to see if the card's expiration month and year precede current month and year
if (($sel_card_year < $year_number) || (($sel_card_year == $year_number) && ($sel_month_number < $month_number)))
{

$error_message = "$error_message<br><br>Your card expiration date precedes today's date.<br>";

}

// checking to see if the user has checked
// the box so that shipping information is
// the same as billing

if ($chk_same_as != "")
{

$txt_ship_first_name = $txt_bill_first_name;
$txt_ship_last_name = $txt_bill_last_name;
$txt_ship_address = $txt_bill_address;
$txt_ship_city = $txt_bill_city;
$sel_ship_state = $sel_bill_state;
$txt_ship_zip = $txt_bill_zip;
$txt_ship_phone = $txt_bill_phone;
$txt_ship_fax = $txt_bill_fax;

}
else
{

if ($txt_ship_first_name == "")
{
$error_message = "$error_message<br>You have omitted your shipping first name.";
}
if ($txt_ship_last_name == "")
{
$error_message = "$error_message<br>You have omitted your shipping last name.";
}
if ($txt_ship_address == "")
{
$error_message = "$error_message<br>You have omitted your shipping address.";
}
if ($txt_ship_city == "")
{
$error_message = "$error_message<br>You have omitted your shipping city.";
}
if ($sel_ship_state == "no_select")
{
$error_message = "$error_message<br>You have omitted your shipping state.";
}
if ($txt_ship_zip == "")
{
$error_message = "$error_message<br>You have omitted your shipping zipcode.";
}

}
// displaying an error message on the page
// that gives the user appropriate feedback about what is missing

if ($error_message != "")
{

$_SESSION["error_message"] = $error_message;
header("location:profile_update.php");

}
// if all the required inputs are present then
// we need to create the session_variables
// that will retain the inputs for use throughout the session
// and write these inputted values to our profile table

else
{

// first we establish the session variables
// that can be accessed everyplace else in the site
// for this user session

session_start();
$_SESSION["active_profile"] = "active";
$_SESSION["email_original"] = $txt_email;
$_SESSION["password_original"] = $txt_password;
$_SESSION["bill_first_name"] = $txt_bill_first_name;
$_SESSION["bill_last_name"] = $txt_bill_last_name;
$_SESSION["bill_address"] = $txt_bill_address;
$_SESSION["bill_city"] = $txt_bill_city;
$_SESSION["bill_state"] = $sel_bill_state;
$_SESSION["bill_zip"] = $txt_bill_zip;
$_SESSION["bill_phone"] = $txt_bill_phone;
$_SESSION["bill_fax"] = $txt_bill_fax;
$_SESSION["ship_first_name"] = $txt_ship_first_name;
$_SESSION["ship_last_name"] = $txt_ship_last_name;
$_SESSION["ship_address"] = $txt_ship_address;
$_SESSION["ship_city"] = $txt_ship_city;
$_SESSION["ship_state"] = $sel_ship_state;
$_SESSION["ship_zip"] = $txt_ship_zip;
$_SESSION["ship_phone"] = $txt_ship_phone;
$_SESSION["ship_fax"] = $txt_ship_fax;
$_SESSION["card_name"] = $txt_card_name;
$_SESSION["card_type"] = $sel_card_type;
$_SESSION["card_number"] = $txt_card_number;
$_SESSION["expiration_month"] = $sel_card_month;
$_SESSION["expiration_year"] = $sel_card_year;
// now we should write the inputs to the profile table
// connecting to the database on battcave.com

$link = mysql_connect($host, $user, $password);
// creating the query string
$query_string = "UPDATE profile SET
email = '$txt_email',
password = '$txt_password',
bill_first_name ='$txt_bill_first_name',
bill_last_name = '$txt_bill_last_name',
bill_address ='$txt_bill_address',
bill_city = '$txt_bill_city',
bill_state ='$sel_bill_state',
bill_zipcode = '$txt_bill_zip',
bill_phone = '$txt_bill_phone',
bill_fax = '$txt_bill_fax',
ship_first_name = '$txt_ship_first_name',
ship_last_name = '$txt_ship_last_name',
ship_address ='$txt_ship_address',
ship_city = '$txt_ship_city',
ship_state ='$sel_ship_state',
ship_zipcode ='$txt_ship_zip',
ship_phone ='$txt_ship_phone',
ship_fax ='$txt_ship_fax',
card_type ='$sel_card_type',
card_number ='$txt_card_number',
expiration_month ='$sel_card_month',
expiration_year ='$sel_card_year',
card_name ='$txt_card_name'
WHERE (email = '$txt_original_email' AND password = '$txt_original_password')";

// executing the SQL statement
mysql_db_query($db_name, $query_string, $link);

mysql_close($link);
header("location:profile_updated.php");

}
?>
</body>
</html>

 

This processing script can be looked at as having two overall purposes.
  • check to make sure that required inputs are present and pass certain criteria
  • update the shopper's profile if they do pass input validation

Now we start the more detailed code discussion.

  • we start by ensuring we have the original email and password the user inputted to access these forms via the session variables
  • then we initialize the error_message to be blank
  • then we start with the input validation
    • First we test to see if the email entry is blank
      • if it is we append appropriate words to the error_message
      • if it isn't we connect to our profile table and query to find out whether the email address has been used before and is different from what the user entered to get into the form update pages
        • if the email address has been used before by someone else we append this to our error_message
    • Then we test to see if the password is blank
      • if it is we append appropriate words to the error_message
    • Then we test to see if the billing first name is blank
      • if it is we append appropriate words to the error_message
    • Then we test to see if the billing last name is blank
      • if it is we append appropriate words to the error_message
    • Then we test to see if the billing address is blank
      • if it is we append appropriate words to the error_message
    • Then we test to see if the billing city is blank
      • if it is we append appropriate words to the error_message
    • Then we test to see if the billing state has been selected
      • if it isn't we append appropriate words to the error_message
    • Then we test to see if the billing zip is blank
      • if it is we append appropriate words to the error_message
    • Then we test to see if the credit card name is blank
      • if it is we append appropriate words to the error_message
    • Then we test to see if the credit card type is selected
      • if it isn't we append appropriate words to the error_message
    • Now we test to see if the credit card expiration date is after today's date
      • we parse the current month and year
      • we convert the month to appropriate numbers
      • we make a comparison between the current date and the expiration month and year selected by the shopper - if it fails we append an appropriate error message
    • Now we test to see whether the shopper has checked that the shipping address for the profile is the same as the billing address
      • if it is we assign the values appropriately
      • if it isn't, we need to check the shipping entries
        • Then we test to see if the billing first name is blank
          • if it is we append appropriate words to the error_message
        • Then we test to see if the billing last name is blank
          • if it is we append appropriate words to the error_message
        • Then we test to see if the billing address is blank
          • if it is we append appropriate words to the error_message
        • Then we test to see if the billing city is blank
          • if it is we append appropriate words to the error_message
        • Then we test to see if the billing state has been selected
          • if it isn't we append appropriate words to the error_message
        • Then we test to see if the billing zip is blank
          • if it is we append appropriate words to the error_message
    • If the error_message has anything in it
      • we assign the error_message into a session variable
      • redirect the shopper back to the update page
        • to display the error message
        • allow them to revise their earlier efforts
    • Else we
      • assign the session variables that will be used to automatically fill in appropriate values during checkout
      • make a connection to our database profile table
      • update the shopper's inputs into the profile table
      • redirect the shopper to the profile_updated.php

     

Now we need to develop the profile_updated.php which largely gives the shopper feedback about the success of their update.

 

<html>
<head>
<title>Your Profile Has Been Added</title>
</head>
<?php
include("includes/header.php");
?>
<br><br>
<table cellpadding="5" border="0" width="750" align="center">
<tr><td align="center">
<font size="5" color="#990000"><b>Your Profile Has Been Updated</b></font><br>
</td></tr>
<tr><td align="center">
<br><font size="4">You can continue shopping by using the links above.</font><br>
</td></tr>
<tr><td align="center">
<font size="4" color="#a66838">You can update your profile by clicking<br>
on the <a href="profile_access.php">profile</a> link and logging in<br>
using your email address and password</font>
</td></tr>
</table>
</body>
</html>

 

This page will look like the following.