I am using XAMPP for my class and am trying to create a form using PHP that when submitted checks for errors and keeps the user info that they filled out. If no errors when it is submitted then it processes on the same page with a thank you and displays the information that they put in.
I have tried coding this and recoding it about a dozen times now and no matter what I do I fix something and break something else. I was hoping someone could help me figure out where I am going wrong. These are the issues I am still trying to solve.
-
I am getting a Warning: Undefined array key for line 80.
-
The errors are all displaying on the page initially before the user has input information in the form (this should only display after being submitted and only those that are applicable).
-
Once submitted, the errors are processed and are shown (not all are working) on a new page without the form and user information filled out.
-
The drop-downs are not validating and I am not sure how to validate them in an array within an array.
-
The processed/submitted form without errors is processed into a thank you page, but it is not displaying the selected drop-down information.
<?php
//variables
$firstName= $lastName = $email = $phone = $address= $city = $state = $zip = "";
$band = $bands = $color = $colors = $size = $sizes = $style = $styles = "";
$error_ = $message = $messages = $email_from = "";
//arrays
$bands = array ("ACDC", "Journey", "Modest Mouse", "Band of Horses", "Vampire Weekend", "Of Monsters and Men", "Broken Bells", "Phoenix", "Fleetwood Mac", "AJR",);
$colors = array ("Black", "Navy", "Red", "Orange", "Pink", "Yellow", "Green", "Gray", "White", "Purple",);
$sizes = array ("X-Small", "Small", "Medium", "Large", "X-Large", "XX-Large", "XXX-Large",);
$styles = array ("Tank Top", "T-Shirt", "Long Sleeve", "Hoodie", "Sweatshirt", "Jacket",);
$product_vars = array(
array( 'element_name' => 'band', 'title' => 'Band', 'options' => $bands, 'validation' => 'style', 'placeholder' => 'Choose One', ),
array( 'element_name' => 'color', 'title' => 'Color', 'options' => $colors, 'validation' => 'style', 'placeholder' => 'Choose One', ),
array( 'element_name' => 'size', 'title' => 'Size', 'options' => $sizes, 'validation' => 'style', 'placeholder' => 'Choose One', ),
array( 'element_name' => 'style', 'title' => 'Style', 'options' => $styles, 'validation' => 'style', 'placeholder' => 'Choose One', ),
);
$address_vars = array(
array( 'element_name' => 'firstName', 'title' => 'First Name', 'validation' => 'text',),
array( 'element_name' => 'lastName', 'title' => 'Last Name', 'validation' => 'text',),
array( 'element_name' => 'email', 'title' => 'Email Address', 'validation' => 'email',),
array( 'element_name' => 'phone', 'title' => 'Phone Number', 'validation' => 'phone',),
array( 'element_name' => 'address', 'title' => 'Address', 'validation' => 'text',),
array( 'element_name' => 'city', 'title' => 'City', 'validation' => 'text',),
array( 'element_name' => 'state', 'title' => 'State', 'validation' => 'text',),
array( 'element_name' => 'zip', 'title' => 'Zip Code', 'validation' => 'zip',),
);
if(empty($_POST['submit-button'])) //button has NOT been pushed
{
?>
<form action="" method="post">
<?php
foreach($product_vars as $cur_product) {
?>
<label><?php echo $cur_product['title']; ?>: </label>
<select name="<?php echo $cur_product['element_name']; ?>" size="1">
<option><?php echo $cur_product['placeholder']; ?></option>
<?php
foreach($cur_product['options'] as $cur_option)
{
echo "<option value = '".$cur_option."' ".(isset($_POST[$cur_product['element_name']]) && $_POST[$cur_product['element_name']] == $cur_option ? "selected='selected'" : "")."> $cur_option </option>";
}
?>
</select>
<?php
}
foreach($address_vars as $cur_address_field) {
?>
<label><?php echo $cur_address_field['title']; ?>:
<input
type = "text"
value="<?php echo (isset($_POST[$cur_address_field['element_name']]) ? $_POST[$cur_address_field['element_name']] : ''); ?>"
id = "<?php echo $cur_address_field['element_name']; ?>"
placeholder = "<?php echo $cur_address_field['title']; ?>"
name ="<?php echo $cur_address_field['element_name']; ?>">
</label>
<?php } ?>
<input type="reset" class="buttons">
<input type="submit" name = "submit-button" class="buttons">
</form>
<?php
}
if(!empty($_POST['submit-button']))
foreach($product_vars as $validate_field)
{
$messages = validate_field_contents($_POST[$validate_field['element_name']], $validate_field['title'], $validate_field['validation'], $messages);
}
foreach($address_vars as $validate_field)
{
$message = validate_field_contents($_POST[$validate_field['element_name']], $validate_field['title'], $validate_field['validation'], $message);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>T-Shirt Form</title>
<link type="text/css" rel="stylesheet" href="css/style-prod.css">
<script src="..js/script.js" defer></script>
</head>
<body>
<?php
if($message) {
echo '<p>'.$message.'</p>';
}
else {
?>
<!--display processed information here-->
<h3 class="subheaderone">Thank you for your order!</h3><br>
<h3 class = "subheadertwo">Product:</h3>
<div class = "output">
<?php
foreach($product_vars as $cur_product) {
echo "<option value = '".$cur_option."' ".(isset($_POST[$cur_product['element_name']]) && $_POST[$cur_product['element_name']] == $cur_option ? "selected='selected'" : "")."> $cur_option </option>";
}
?>
<h3 class = "subheadertwo">Shipping & Contact Information:</h3>
<?php
foreach($address_vars as $cur_address) {
echo '<p>' . $cur_address['title'] . ': ' . (isset($_POST[$cur_address['element_name']]) ? $_POST[$cur_address['element_name']] : '') . '</p>';
}
?>
</div>
<div class="another">
<nav><a href="./products.php"> Submit Another Form</a></nav>
</div>
<?php } ?>
</body>
</html>
<?php
function validate_field_contents($content, $title, $type, $message, ) {
if($type == 'text') {
$string_exp = "/^[A-Za-z0-9._%-]/";
if(!preg_match($string_exp, $content)) {
$message .= '<li>Please enter a valid ' . strtolower($title) . '.</li>';
}
}
elseif($type == 'email') {
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp, $content)) {
$message .= '<li>Please enter a valid ' . strtolower($title) . '.</li>';
}
}
elseif($type == 'phone'){
$num_exp = "/^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$/";
if(!preg_match($num_exp,$content)) {
{$message .= '<li>Please enter a valid ' . strtolower($title) . ' in the following format (xxx)xxx-xxxx.</li>';}
}
}
elseif($type == 'zip'){
$num_exp = "/^[0-9]{5}$/";
if(!preg_match($num_exp,$content)) {
{$message .= '<li>Please enter a valid ' . strtolower($title) . ' with 5 numbers only.</li>';}
}
}
elseif(empty($_POST['band'])){
{$message .= '<li>Please select a ' . strtolower($title) . '.</li>';}
}
elseif(empty($type == 'color')){
$message .= '<li>Please select a ' . strtolower($title) . '.</li>';
}
elseif(empty($type == 'size')){
$message .= '<li>Please select a ' . strtolower($title) . '.</li>';
}
elseif(empty($type == 'style')){
$message .= '<li>Please select a ' . strtolower($title) . '.</li>';
}
return $message;
}
?>