Modifying the Shipping Information During Checkout

 

Introduction.  Now we want to allow the user to not have to use the shipping address and name in their profile for each order.  This can be quite important since while the billing is almost surely going to be the same as what is in the profile for each order, the shipping information might well chaneg with every order.

So we need to do several things.

  • display the current shipping information within the shopper's profile in a form
  • allow the shopper to change this information
  • make sure to validate the inputs so that required information is present
  • once the shipping information is acceptable all of the shopper's inputs are written to the order_data table

We have developed this page so it is a sticky form to retain whatever the shopper has previously entered.  It also posts to itself so that error messages are at the top of the page in red.  This page should be called checkout_modify_shipping.php.  This page is quite long because it has the sticky form for the shipping information as well as the input validation.

 
 
<html>
<!-- checkout_modify_shipping.php - modifying shipping info for this order -->
<head><title>Modifying the Shipping for this Order</title></head>
<?php
include("includes/header.php");
include("includes/connection_config.php");
?>

<font size="4" color="a66838"><p align="center">You can modify the shipping address
<br>for this order only.
<br>This information will not be posted to your profile.</p></font>
<?php
// if this is the first time the form has been opened we want to initialize the
// entries to what has been obtained from the profile and is contained in the
// appropriate $_SESSION variables

if (!(isset($submit_shipping)))
{

session_start();
$txt_ship_first_name = $_SESSION["ship_first_name"];
$txt_ship_last_name = $_SESSION["ship_last_name"];
$txt_ship_address = $_SESSION["ship_address"];
$txt_ship_city = $_SESSION["ship_city"];
$sel_ship_state = $_SESSION["ship_state"];
$txt_ship_zip = $_SESSION["ship_zip"];
$txt_ship_phone = $_SESSION["ship_phone"];
$txt_ship_fax = $_SESSION["ship_fax"];

}
// 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 = "";
// checking to make sure the form has actually been submitted
if ($submit_shipping != "")
{

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 != "")
{

echo ("<br><center><font color=ff0000 size=4> $error_message </font></center>");

}
// 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();
$id_basket = $_SESSION['id_basket'];
$email = $_SESSION["email_original"];
$bill_first_name = $_SESSION["bill_first_name"];
$bill_last_name = $_SESSION["bill_last_name"];
$bill_address = $_SESSION["bill_address"];
$bill_city = $_SESSION["bill_city"];
$bill_state = $_SESSION["bill_state"];
$bill_zip = $_SESSION["bill_zip"];
$bill_phone = $_SESSION["bill_phone"];
$bill_fax = $_SESSION["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;
$card_name = $_SESSION["card_name"];
$card_type = $_SESSION["card_type"];
$card_number = $_SESSION["card_number"];
$expiration_month = $_SESSION["card_month"];
$expiration_year = $_SESSION["card_year"];
// now we should write the inputs to the order_status table
// connecting to the database on battcave.com
$link = mysql_connect($host, $user, $password);
// creating the query string
$query_string = "INSERT INTO order_data
VALUES ('0',
$id_basket,
'$email',
'$bill_first_name',
'$bill_last_name',
'$bill_address',
'$bill_city',
'$bill_state',
'$bill_zip',
'$bill_phone',
'$bill_fax',
'$txt_ship_first_name',
'$txt_ship_last_name',
'$txt_ship_address',
'$txt_ship_city',
'$sel_ship_state',
'$txt_ship_zip',
'$txt_ship_phone',
'$txt_ship_fax',
'$card_type',
'$card_number',
'$expiration_month',
'$expiration_year',
'$card_name',
Now( ))";

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

// get the auto-incremented id_order_data using a PHP function
// put it into a session variable for use elsewhere
$_SESSION["id_order_data"] = mysql_insert_id();

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

}

}
?>
<br>
<div align="center">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<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 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">*Name:</font></td>
<td width="387">
<input name="txt_ship_first_name" size="15" style="font-size: 14pt" value = "<?php echo $txt_ship_first_name; ?>">
<input name="txt_ship_last_name" size="20" style="font-size: 14pt" value = "<?php 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 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 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 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 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 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">
<input type="submit" value="Submit" name="submit_shipping" style="font-size: 14pt; font-weight: bold"></td>
</tr>
</table>
<p>&nbsp;</p>
</form>
</div>

</body>
</html>

 

In many respects this is just the capabilities to update the shipping so the code structure will be very similar to what we have done in other places when updating the profile.  We only need to focus on the shipping information and form.

A general code description follows.

  • if this is the first time the page is accessed then we need to fill in the form with entries from the profile via the current $_SESSION variables
  • if this form has been submitted we need to start assessing the validity of the entries
    • we create a blank error_message
    • we append new messages to this error_message that depend on whether certain things are left blank
    • if the error_message isn't blank
      • we display the error_message at the top of the sticky form to help the shopper fill in blank entries
    • if the error_message is blank this implies there were no entry errors and we can
      • coalesce this information into the $_SESSION variables
      • reacquire the other profile information from the $_SESSION variables
      • connect to the database adn insert the information into the order_data table
      • redirect the shopper to the next PHP page checkout_completed.php
  • finally we have the sticky form which posts to itself

This leads us to the last page of our website.