The code below is to be used to receive information from an html form, format it and send it via email. It then redirects to a page where a confirmation is displayed. But, it doesn’t seem to be functioning properly so I’m hoping I could get some pointers as to why that is.
PHP:
<?php
$errorMsg = "";
$firstName = "";
$lastName = "";
$phone = "";
$email = "";
$repair = "";
$color = "";
if($_SERVER["REQUEST_METHOD"] == "POST"){
if (empty($_POST['firstname'])){
$errorMsg .= "Please enter your first name.";
}
else{
$firstName = $_POST['firstname'];
}
if (empty($_POST['lastname'])){
$errorMsg .= "Please enter your last name.";
}
else{
$lastName = $_POST['lastname'];
}
if (empty($_POST['phone'])){
$errorMsg .= "Please enter your phone number.";
}
else{
$phone = $_POST['phone'];
}
if (empty($_POST['email'])){
$errorMsg .= "Please enter your email address.";
}
else{
$email = $_POST['email'];
}
$repair = $_POST['repairs'];
$color = $_POST['color'];
$fullName = $firstName . ' ' . $lastName;
$subject = $lastName . ', ' . $firstName . ' ' . $repair;
$sendTo = "email@email.com";
$body = "";
$body .= "Name: ";
$body .= $fullName;
$body .= "\n";
$body .= "Email: ";
$body .= $email;
$body .= "\n";
$body .= "Phone: ";
$body .= $phone;
$body .= "\n";
$body .= "Phone color: ";
$body .= $color;
$body .= "\n";
$body .= "Repairs: ";
$body .= $repairs;
mail($sendTo, $subject, $body);
header('Location: website');
}
?>
HTML:
<form action="form_handling.php" method="POST">
<div class="row" style="display:flex">
<div class="column" style="flex:70%">
<input type="checkbox" name="color" value="spaceGray" style="margin-left:15%"> Space Gray
<br>
<input type="checkbox" name="color" value="silver" style="margin-left:15%"> Silver
<br>
<input type="checkbox" name="color" value="gold" style="margin-left:15%"> Gold
</div>
<div class="column" style="flex:50%">
<input type="checkbox" name="repair" value="screen"> Charge Port Replacement
<br>
<input type="checkbox" name="repair" value="battery"> Battery Replacement
<br>
<input type="checkbox" name="repair" value="homeButton"> Home Button Replacement
<br>
<input type="checkbox" name="repair" value="screen"> Screen Replacement
</div>
</div>
<br><hr style="margin-top:5%;border-color:black;margin-bottom:5%">
<br>
<h2 style="font-family:roboto;color:#f3892b;text-align:center;font-weight:300">CONTACT INFO</h2>
<label for="firstname">First Name</label>
<input type="text" id="firstname" name="firstname" placeholder="Your name.." required>
<br><br>
<label for="lastname">Last Name</label>
<input type="text" id="lastname" name="lastname" placeholder="Your last name.." required>
<br><br>
<label for="phone">Phone Number</label>
<input type="text" id="phone" name="phone" placeholder="Your phone number.." required>
<br><br>
<label for="email">Email Address</label>
<input type="text" id="email" name="email" placeholder="Your email address.." required>
<br><br>
<div style="text-align:center">
<input type="submit" value="Submit" style="background-color:#f3892b;font-family:roboto">
</div>
</form>