联系我们.PHP文件的问题表格...提交表格后,信息消失

时间:2015-02-27 12:12:32

标签: php forms email contact

我被要求加入一个“联系我们”'我们网站的表单,用于生成发送给该协会主要电子邮件地址的电子邮件。我以前从来没有完成过这项任务,而且很快意识到我在脑海中,试图学习PHP的语言,并且我正在寻求帮助。

为了更好地帮助我,这里是表单的代码,包括HTML,CSS和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.
}

?>

目前,表单完美呈现,但信息永远不会消失,永远丢失。

在理想的世界中,我们非常希望PHP能够将包含所有信息的电子邮件发送到我们的主要协会的电子邮件中,然后打开我们的&#34;谢谢你&#34; HTML页面或我们的&#34;糟糕&#34;页面,取决于电子邮件是否成功。 我知道我很遗憾,很可能,但任何帮助都会非常感激!

0 个答案:

没有答案