vendredi 27 février 2015

Issues with .PHP file for Contact Us form...once form is submitted, information disappears

I've been asked to incorporate a 'Contact Us' form for our site that generates an email to the association's main email address. I'd never had this task before and came to realize all too quickly that I am in over my head, trying to learn the language of PHP and I'm looking for some help.


To better assist me, here is the code for the form, including the HTML, CSS, and PHP:


HTML



<form action="contactus.php" method="post" target="(a Thank You page on our website)">
First Name: <input type="text" name="fname">
<span class="error">* <?php echo $fnameErr;?></span>
<br><br>
Last Name: <input type="text" name="lname">
<span class="error">* <?php echo $lnameErr;?></span>
<br><br>
Phone Number: <input type="text" name="phone">
<br><br>
E-mail Address: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Favorite item: <input type="text" name="favorite">
<br><br>
Membership # (if applicable): <input type="text" name="member">
<br><br>
Comment/Question/Suggestion: <textarea name="comment" rows="5" cols="40"></textarea>
<span class="error">* <?php echo $commentErr;?></span>
<br/>
<br/>
<input type="submit" name="submit" value="Submit">
</form>


CSS



form {
/* Center the form on the page */
width: 400px;
/* Outlines the form */
padding: 1em;
border: 1px solid #ffd700;
border-radius: 1em;
color: #e30000;
font-weight: bold;
}

form div + div {
margin-top: 1em;
}

label {
/* Makes sure that all labels are properly aligned */
display: inline-block;
width: 300px;
text-align: left;
color: ffd700;
}

input, textarea {
/* Same font settings for all text fields */
font: 1em sans-serif;
/* Gives the same size to all text fields */
width: 300px;
-moz-box-sizing: border-box;
box-sizing: border-box;
/* Makes border similar to text field border */
border: 1px solid #ffd700;
}

input:focus, textarea:focus {
/* Highlights on active elements */
border-color: #ffd700;
}

textarea {
/* Align text fields to the names of field */
vertical-align: top;
/* To see the text */
height: 5em;
}

.button {
/* Moves the buttons to the same position of the text fields */
padding-left: 90px;
}

.button {
/* This extra margin represent roughly the same space as the space
between the labels and their text fields */
margin-left: .5em;
}


PHP



<?php

$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["fname"])) {
$nameErr = "First Name is required";
} else {
$fname = test_input($_POST["fname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
$fnameErr = "Only letters and white space allowed";
}
}

if (empty($_POST["lname"])) {
$lnameErr = "Last Name is required";
} else {
$lname = test_input($_POST["lname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
$lnameErr = "Only letters and white space allowed";
}
}

if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}

if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
}

if(isset($_POST['submit'])){
$to = "(our main email address";
$from = $_POST['email'];
$first_name = $_POST['fname'];
$last_name = $_POST['lname'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $fname . " " . $lname . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Thank you for your submission to (our club name) " . $fname . ", a member of (our club) will be in contact with you shortly!";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}

?>


Currently, the form submits perfectly, however the information never goes anywhere and is lost forever.


In an ideal world, we'd love for the PHP to send an email with all of the information to our main association's email and then either open our "Thank You" HTML page or our "Oops" page, depending on if the email was successful. I know I am missing a lot, most likely, but any help would be greatly appreciated!


Aucun commentaire:

Enregistrer un commentaire