Created
June 17, 2014 01:52
-
-
Save doug7410/81e6ec8b1bf19db741ce to your computer and use it in GitHub Desktop.
lab 8 objective 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//get the arrays for US states, Canadian Provences, and both | |
// the array names are $us_states, $canadian_provences, $state_array | |
require('state_list.php'); | |
// create an <option> list | |
// | |
// takes in a $key => $value array and the name of the <select> element | |
// and creates a dropdown list. It uses the name of the <select> element | |
// to check if the $_REQUEST var for the <slect> is the currnet | |
// item in the array. If it is, it adds 'selected' to the current <option> | |
function option_list($option_array, $list_name){ | |
$list = '<option value="">Please choose...</option>'; | |
foreach ($option_array as $key => $value ) { | |
if($_REQUEST[$list_name] == $key){ | |
$list .= '<option value="'.$key.'" selected>'.$value.'</option>'; | |
}else{ | |
$list .= '<option value="'.$key.'">'.$value.'</option>'; | |
}; | |
} | |
echo $list; | |
} | |
// selects the correct radio button | |
// takes in the name of a $_POST var and a string | |
// if the $_POST var and the string match it echos 'checked' | |
function check_radio($post_var_name, $value){ | |
if($_POST[$post_var_name] == $value): echo ' checked'; endif; | |
} | |
// this function just makes it a little quicker to echo the value of a $_POST variable | |
function post($name){ | |
echo $_POST[$name]; | |
} | |
// connect to the datacase | |
$host = "localhost"; | |
$socket = "/users/dsteinbe/mysql-php/data/mysql.sock"; | |
$port = 0; | |
$user = "doug"; | |
$pw = "snowball420"; | |
$database = "php_sql_2"; | |
$db = new mysqli($host,$user,$pw,$database,$port,$socket) | |
or die("Cannot connect to mySQL."); | |
//get form variables and put them in an array for the SQL***********************// | |
// the variables created are $company_name, $address, $city, $state, $zip, $country | |
extract($_POST); | |
//start validation***************************************************************************************// | |
// if the form has been submitted start validation | |
if($_POST['submited']) : | |
// check if anything is missing | |
if(!$_POST['company_name']) : $error_message .= "Please enter a company name.<br>" ; endif; | |
if(!$_POST['address'] ) : $error_message .= "Please enter an address.<br>" ; endif; | |
if(!$_POST['city'] ) : $error_message .= "Please enter a city.<br>" ; endif; | |
if(!$_POST['state'] ) : $error_message .= "Please enter a state.<br>" ; endif; | |
if(!$_POST['zip'] ) : $error_message .= "Please enter a zip code.<br>" ; endif; | |
if(!$_POST['country'] ) : $error_message .= "Please enter a country.<br>" ; endif; | |
// validate the company name | |
if($_POST['company_name']) : | |
if (strlen($company_name) > 25) : | |
$error_message .= "Company name must be 25 characters or less. <br>"; | |
endif; | |
endif; | |
// validate the address | |
if($_POST['address']) : | |
if ( strlen($address) > 100 ) : | |
$error_message .= "Address must be less than 100 characters. <br>"; | |
elseif ( strlen( preg_replace("/[^A-Za-z]/", '', $address) ) < 2 ) : | |
$error_message .= "Address should have at least two letters<br>"; | |
elseif (preg_match("/[^A-Za-z0-9\-#.' ]/i", $address)) : | |
$error_message .= "Address should contain only letters, numbers, the # sign, apostrophes, hyphens, periods and spaces<br>"; | |
endif; | |
endif; | |
// validate the city | |
if($_POST['city']) : | |
if (strlen($city) > 25) : | |
$error_message .= "City must be 25 characters or less. <br>"; | |
elseif (preg_match("/[^A-Za-z-' ]/i", $city)) : | |
$error_message .= "City should contain only letters, hyphens, apostrophes, or spaces. <br>"; | |
endif; | |
endif; | |
// validate the state | |
if($_POST['state']) : | |
if (!array_key_exists($state, $state_array)) : | |
$error_message .= "You didn't enter a valid state.<br>"; | |
// this part checks if the state choosen in in the selected country | |
elseif ($country == 'US') : | |
if(!array_key_exists($state, $us_states)) : | |
$error_message .= "The state you selected is not is the US<br>"; | |
endif; | |
elseif ($country == 'CA') : | |
if(!array_key_exists($state, $canadian_provences)) : | |
$error_message .= "The state you selected is not is Canada<br>"; | |
endif; | |
endif; | |
endif; | |
// validate the zip | |
if ($_POST['zip']) : | |
// this code came from http://www.pixelenvision.com/1708/zip-postal-code-validation-regex-php-code-for-12-countries/ | |
$ZIPREG=array( | |
"US"=>"^\d{5}([\-]?\d{4})?$", | |
"UK"=>"^(GIR|[A-Z]\d[A-Z\d]??|[A-Z]{2}\d[A-Z\d]??)[ ]??(\d[A-Z]{2})$", | |
"DE"=>"\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3}))\b", | |
"CA"=>"^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\ {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$", | |
"FR"=>"^(F-)?((2[A|B])|[0-9]{2})[0-9]{3}$", | |
"IT"=>"^(V-|I-)?[0-9]{5}$", | |
"AU"=>"^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$", | |
"NL"=>"^[1-9][0-9]{3}\s?([a-zA-Z]{2})?$", | |
"ES"=>"^([1-9]{2}|[0-9][1-9]|[1-9][0-9])[0-9]{3}$", | |
"DK"=>"^([D-d][K-k])?( |-)?[1-9]{1}[0-9]{3}$", | |
"SE"=>"^(s-|S-){0,1}[0-9]{3}\s?[0-9]{2}$", | |
"BE"=>"^[1-9]{1}[0-9]{3}$" | |
); | |
if (strlen($zip) > 10) : | |
$error_message .= "Zip must be 10 characters or less. <br>"; | |
elseif ($country == 'CA') : | |
if (!preg_match("/".$ZIPREG['CA']."/i",$zip)) : | |
$error_message .= "You enterd an invalid Canadian postal code<br>"; | |
endif; | |
elseif ($country == 'US') : | |
if (!preg_match("/".$ZIPREG['US']."/i",$zip)) : | |
$error_message .= "You enterd an invalid US zip code<br>"; | |
endif; | |
endif; | |
endif; | |
// validate the country | |
if($_POST['country']) : | |
$allowed_country = array('US', 'CA'); | |
if (!in_array($country, $allowed_country) ) : | |
$error_message .= "Country must be US or CA<br>"; | |
endif; | |
endif; | |
//process the form ************************************** | |
//create the string for SQL to use | |
$input_array = array($company_name, $address, $city, $state, $zip, $country); | |
$insert_string = "''"; | |
foreach ($input_array as $key => $value) { | |
$insert_string .= ",'".$db->real_escape_string($value)."'"; | |
} | |
if ($error_message ) { | |
echo "Error! <br>".$error_message; | |
} | |
else { | |
$command = "INSERT INTO sales_customers VALUES (".$insert_string.")"; | |
$result = $db->query($command); | |
if($result): | |
echo "A new company has been successfully added!"; | |
else: | |
echo "There's a problem!" . mysqli_error($db); | |
endif; | |
} | |
$db->close(); | |
endif; // end validation and form processing | |
?> | |
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Add Customer</title> | |
</head> | |
<body> | |
<h3>Add customer to sales database</h3> | |
<form method="POST" action="<?php echo getenv('SCRIPT_NAME'); ?>"> | |
<input type="hidden" name="submited" value="1"> | |
<table> | |
<tr> | |
<td align="right">Company Name:</td> | |
<td align="left"> | |
<input type="text" size="50" maxlength="50" name="company_name" value="<?php post('company_name'); ?>"> | |
</td> | |
</tr> | |
<tr> | |
<td align="right">Address:</td> | |
<td align="left"> | |
<input type="text" size="100" maxlength="100" name="address" value="<?php post('address'); ?>"> | |
</td> | |
</tr> | |
<tr> | |
<td align="right">City:</td> | |
<td align="left"> | |
<input type="text" size="25" maxlength="25" name="city" value="<?php post('city'); ?>"> | |
State/Province: | |
<select name="state"> | |
<?php option_list($state_array , 'state'); //create a dropdown list from the states array ?> | |
</select> | |
Zip Code: | |
<input type="text" size="10" maxlength="10" name="zip" value="<?php post('zip'); ?>"> | |
</td> | |
</tr> | |
<tr> | |
<td align="right" valign="top">Country:</td> | |
<td align="left"> | |
<input type="radio" name="country" value="US" <?php check_radio('country', 'US'); ?> >United States<br> | |
<input type="radio" name="country" value="CA" <?php check_radio('country', 'CA') ?> >Canada | |
</td> | |
</tr> | |
<tr> | |
<td colspan="2" align="right"> | |
<input type="submit" value="SUBMIT"> | |
</td> | |
</tr> | |
</table> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment