I wrote my first template, but didn't include any styling. What do you think? What's good what's bad?
<?php
/*
Template Name: Email product info
*/
//checks required fields
$allFormPosts = array("address", "email", "xsize", "ysize", "zsize");
foreach($allFormPosts as $formPost){
if(isset($_POST[$formPost])){
${$formPost} = trim($_POST[$formPost]);
}else{
$formNotValid = true;
break;
}
}
if(!isset($formNotValid)){
//checks address
if(!strlen($address)){
$hasError = true;
$addressError = "You forgot to enter address.";
}
//checks email
if(!strlen($email)){
$hasError = true;
$emailError = "You didn't enter email address";
}
else if(!is_email($email)){
$hasError = true;
$emailError = "Email address is not valid.";
}
//check x
if(!strlen($xsize)){
$hasError = true;
$xsizeError = "You forgot to enter \"x\" size.";
}
else if(!is_numeric($xsize) || $xsize <= 0){
$hasError = true;
$xsizeError = "\"$xsize\" is not valid.";
}
//checks y
if(!strlen($ysize)){
$hasError = true;
$ysizeError = "You forgot to enter \"y\" size.";
}
else if(!is_numeric($ysize) || $ysize <= 0){
$hasError = true;
$ysizeError = "\"$ysize\" is not valid.";
}
//checks z
if(!strlen($zsize)){
$hasError = true;
$zsizeError = "You forgot to enter \"z\" size.";
}
else if(!is_numeric($zsize) || $zsize <= 0){
$hasError = true;
$zsizeError = "\"$zsize\" is not valid.";
}
//sends email if form is valid
if(!isset($hasError)){
$subject = "Package";
$body = "Package is being sent to $address. Package size is $xsize-$ysize-$zsize cm";
mail($email, $subject, $body);
}
}
?>
<?php get_header(); ?>
<form action="<?php the_permalink(); ?>" id="productForm" method="post">
<?php
if(isset($hasError) || isset($formNotValid))
echo "<p>There has been an error.</p>";
?>
<table>
<tr>
<td>Address*:</td>
<td><input type="text" name="address" value="<?php if(isset($address) && !isset($addressError)) echo $address;?>" required="required" placeholder="Enter place">
<?php if(isset($addressError)){
echo $addressError;
}
?>
</td>
</tr>
<tr>
<td>Email*:</td>
<td><input type="email" name="email" value="<?php if(isset($email) && !isset($emailError)) echo $email;?>" required="required" placeholder="Enter email">
<?php if(isset($emailError)){
echo $emailError;
}
?>
</td>
</tr>
<tr>
<td>Size*:</td>
<td><input type="number" size="3" placeholder="x" name="xsize" required="required" min="1" value="<?php if(isset($xsize) && !isset($xsizeError)) echo $xsize;?>">
<input type="number" size="3" placeholder="y" name="ysize" required="required" min="1" value="<?php if(isset($ysize) && !isset($ysizeError)) echo $ysize;?>">
<input type="number" size="3" placeholder="z" name="zsize" required="required" min="1" value="<?php if(isset($zsize) && !isset($zsizeError)) echo $zsize;?>">cm
<?php if(isset($xsizeError)){
echo $xsizeError;
}
if(isset($ysizeError)){
echo $ysizeError;
}
if(isset($zsizeError)){
echo $zsizeError;
}
?>
</td>
</tr>
</table>
<button type="submit" value="submitted">Submit</button>
</form>
<?php get_footer(); ?>