联系表单重定向到PHP错误页面

时间:2017-08-26 13:47:12

标签: php html email

我正在尝试将我的HTML表单提交到PHP脚本(工作正常),但PHP应该将其发送到电子邮件。相反,它最终会出现在错误页面上。所以我认为HTML代码不是问题,PHP中必定存在错误。任何人都可以帮我解决这个错误吗?

更新:现在没有更多错误,但它会将我重定向到空白网站..

更新了HTML:

<form method="post" name="myform" action="form-to-email.php" id="myform">
      <div class="form-group col-sm-5 col-sm-offset-1"> <!-- Date input -->
        <label class="control-label" for="date1">Date of arrival</label>
        <input class="form-control" id="date" name="date1" placeholder="MM/DD/YYY" type="text"/>
      </div>
       <div class="form-group col-sm-5 col-sm-offset-1"> <!-- Date input -->
         <label class="control-label" for="date2">Date of departure</label>
         <input class="form-control" id="date2" name="date2" placeholder="MM/DD/YYY" type="text"/>
      </div>
      <div class="form-group col-sm-5 col-sm-offset-1">
    <label for="adults">Adults</label>
    <select class="form-control" name="adults">
        <option>0</option>
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
        <option>6</option>
        <option>7</option>
        <option>8</option>
      </select>
  </div>
    <div class="form-group col-sm-5 col-sm-offset-1">
    <label for="children">Children (under 14 years)</label>
    <select class="form-control" name="children">
         <option>0</option>
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
        <option>6</option>


      </select>
  </div><br><br>
  <p class="location text-center col-xs-12"> Please enter your name and address:</p>
   <div class="form-group col-sm-6 col-sm-offset-3">
    <label for="title">Title</label>
    <select class="form-control" name="title">
         <option>Mr.</option>
        <option>Mrs.</option>

      </select>
  </div>
     <div class="form-group col-sm-6 col-sm-offset-3">
    <label for="lastname">First name</label>
    <input type="text" class="form-control" id="firstname" name="firstname">
  </div>
   <div class="form-group col-sm-6 col-sm-offset-3">
    <label for="lastname">Last name</label>
    <input type="text" class="form-control" id="lastname" name="lastname">
  </div>
   <div class="form-group col-sm-3 col-sm-offset-3">
    <label for="lastname">Adress </label>
    <input type="text" class="form-control" id="adress" name="adress">
  </div>
   <div class="form-group col-sm-3 col-sm-offset-1">
    <label for="lastname">N°</label>
    <input type="text" class="form-control" id="n°" name="n°">
  </div>
    <div class="form-group col-sm-3 col-sm-offset-3">
    <label for="lastname">City </label>
    <input type="text" class="form-control" id="city" name="city">
  </div>
   <div class="form-group col-sm-3 col-sm-offset-1">
    <label for="zip">ZIP</label>
    <input type="text" class="form-control" id="zip" name="zip">
  </div>
   <div class="form-group col-sm-6 col-sm-offset-3">
    <label for="country">Country</label>
    <input type="text" class="form-control" id="country" name="country">
  </div>
     <p class="location text-center col-xs-12"> Please enter your contact details:</p>
      <div class="form-group col-sm-6 col-sm-offset-3">
    <label for="email">E-mail</label>
    <input type="text" class="form-control" id="email" name="email">
  </div>
     <div class="form-group col-sm-6 col-sm-offset-3">
       <label for="mobile">Mobile number</label>
       <input type="text" class="form-control" id="mobile" name="mobile">
  </div> <div class="form-group col-sm-6 col-sm-offset-3">
       <label for="comments">Comments</label>
       <input type="text" class="form-control" id="comments" name="comments" style="height: 100px;">
  </div>
      <div class="form-group"> <!-- Submit button -->
        <button class="btn btn-primary col-sm-offset-5" name="submit" id="submit" value="submit" type="submit">Submit</button>
      </div>
     </form>

更新了PHP:

   <?php
if(isset($_POST['submit'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "someones@email.com";
    $email_subject = "Booking request";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }


    // validation expected data exists
    if(!isset($_POST['date1']) ||
        !isset($_POST['date2']) ||
        !isset($_POST['adults']) ||
        !isset($_POST['children']) ||
        !isset($_POST['title']) ||
        !isset($_POST['firstname']) ||
        !isset($_POST['lastname']) ||
        !isset($_POST['adress']) ||
        !isset($_POST['n°']) ||
        !isset($_POST['city']) ||
        !isset($_POST['zip']) ||
        !isset($_POST['country']) ||
        !isset($_POST['email']) ||
        !isset($_POST['mobile'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }



    $date1 = $_POST['date1']; // required
    $date2 = $_POST['date2']; // required
    $adults = $_POST['adults']; // required
    $children = $_POST['children']; // required
    $title = $_POST['title']; // required
    $firstname = $_POST['firstname']; // required
    $lastname = $_POST['lastname']; // required
    $adress = $_POST['adress']; // required
    $n° = $_POST['n°']; // required
    $city = $_POST['city']; // required
    $zip = $_POST['zip']; // required
    $country = $_POST['country']; // required
    $email = $_POST['email']; // required
    $mobile = $_POST['mobile']; // not required
    $comments = $_POST['comments']; // not required


    $error_message = "Please fill all required fields.";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$firstname)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';

  if(!preg_match($string_exp,$lastname)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }

  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }

  if(strlen($error_message) > 0) {
    died($error_message);
  }

    $email_message = "Form details below.\n\n";


    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }



    $email_message .= "Date of Arrival: ".clean_string($date1)."\n";
    $email_message .= "Date of Departure: ".clean_string($date2)."\n";
    $email_message .= "Adults: ".clean_string($adults)."\n";
    $email_message .= "Children: ".clean_string($children)."\n";
    $email_message .= "Title: ".clean_string($title)."\n";
    $email_message .= "First Name: ".clean_string($firstname)."\n";
    $email_message .= "Last Name: ".clean_string($lastname)."\n";
    $email_message .= "Adress: ".clean_string($adress)."\n";
    $email_message .= "N°: ".clean_string($n°)."\n";
    $email_message .= "City: ".clean_string($city)."\n";
    $email_message .= "ZIP: ".clean_string($zip)."\n";
    $email_message .= "Country: ".clean_string($country)."\n \n";
    $email_message .= "Email: ".clean_string($email)."\n";
    $email_message .= "Mobile: ".clean_string($mobile)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email, $email_subject, $email_message, $headers); 
header('Location: thank-you.html'); 

?>

<!-- include your own success html here -->



<?php
  }
}
?>

1 个答案:

答案 0 :(得分:0)

代码

if (isset($_POST["submit"]))
表单发回时

返回false,因为虽然您有一个name="submit"的按钮,但它没有value属性。因此,提交表单时“submit”变量为空,导致isset返回false。

只需添加

value="submit"

到您的按钮,例如

<button class="btn btn-primary col-sm-offset-5" name="submit" id="submit" type="submit" value="submit">Submit</button>

N.B。 value没有要求具有任何特定内容。 value="x"也可以正常工作 - 所有代码测试都是值的存在。