PHP联系表格无法正确提交

时间:2018-07-20 00:51:13

标签: javascript php html forms

我在

上的网站上使用了简单的PHP联系人表格。

http://www.colin-grant.ca/speedvale

但是当我将联系表单迁移到http://www.speedvale.com时,它给我的错误是:“出了点问题:('。

我在下面发布了我的代码供您查看,任何帮助都将非常有用-因为我不是PHP方面的佼佼者。

HTML

<section id="contact">
<div class="container">
  <div class="row">
     <div class="col-lg-12 mx-auto">
        <div class="row">
           <div class="col-sm-12 col-sm-offset-3">
              <div class="well">
                 <h1>Contact Us</h1>
                 <p class="text-center">We are always interested in hearing about how we can improve your visit. If you need more information regarding our packages, or if you wish to set up a detailing appointment, toss us a message below and we will get back to you ASAP.</p>
                 <hr>
                 <form role="form" id="contactForm" data-toggle="validator" class="shake">
                    <div class="row">
                       <div class="form-group col-sm-6">
                          <label for="fname" class="h4">First Name</label>
                          <input type="text" class="form-control" id="fname" placeholder="Please enter your First Name" required data-error="Please Enter First Name">
                          <div class="help-block with-errors"></div>
                       </div>
                       <div class="form-group col-sm-6">
                          <label for="lname" class="h4">Last Name</label>
                          <input type="text" class="form-control" id="lname" placeholder="Please enter your Last Name" required data-error="Please Enter Last Name">
                          <div class="help-block with-errors"></div>
                       </div>
                    </div>
                    <div class="row">
                       <div class="form-group col-sm-6">
                          <label for="email" class="h4">Email</label>
                          <input type="text" class="form-control" id="email" placeholder="Whats your e-mail?" required data-error="Please enter email">
                          <div class="help-block with-errors"></div>
                       </div>
                       <div class="form-group col-sm-6">
                          <label for="phone" class="p">Phone</label>
                          <input type="text" class="form-control" id="phone" placeholder="Whats your phone number?" required data-error="Please enter Phone Number">
                          <div class="help-block with-errors"></div>
                       </div>
                    </div>
                    <div class="row">
                       <div class="form-group col-sm-12">
                          <label for="email" class="h4">Service</label>
                          <select class="form-control" id="service" placeholder="Service"name="services">
                             <option value="" selected disabled>Please choose a service</option>
                             <option value="Gold Package ($79.99 / $99.99)">Gold Package ($79.99 / $99.99)</option>
                             <option value="Platinum 1 Package ($144.99 / $174.99)">Platinum 1 Package ($144.99 / $174.99)</option>
                             <option value="Platinum 2 Package ($199.99 / $229.99)">Platinum 2 Package ($199.99 / $229.99) </option>
                             <option value="VIP Package ($329.99 / $359.99)">VIP Package ($329.99 / $359.99)</option>
                             <option value="Express Package ($29.99 / $39.99)">Express Package ($29.99 / $39.99)</option>
                             <option value="Interior Package ($119.99 / $139.99)">Interior Package ($119.99 / $139.99)</option>
                             <option value="Exterior Package ($84.99 / $134.99)">Exterior Package ($84.99 / $134.99)</option>
                             <option value="Additional Services">Additional Services</option>
                          </select>
                          <div class="help-block with-errors"></div>
                       </div>
                    </div>
                    <div class="form-group">
                       <label for="message" class="p ">Message</label>
                       <textarea id="message" class="form-control" rows="5" placeholder="Please leave details here about the make and model of your car, and any additional information we can use to make your apppointment or request better! Thank you" required></textarea>
                       <div class="help-block with-errors"></div>
                    </div>
                    <button style="width:100%; padding: 10px; margin-bottom: 20px;" type="submit" id="form-submit" class="btn btn-success btn-lg pull-right ">Submit</button>
                    <p class="text-center" style="font-size: 14px;">Please do your best to fill out the form below with as much information as possible so we can get started right away on your inquiry!</p>
                    <p class="text-center" style="font-size:14px;">Thank you from all of us here at Speedvale Auto Detailing.</p>
                    <div id="msgSubmit" class="h3 text-center hidden"></div>
                    <div class="clearfix"></div>
                 </form>
              </div>
           </div>
        </div>
     </div>
  </div>
</div>
</section>

PHP

    <?php

$errorMSG = "";

// FIRST NAME
if (empty($_POST["fname"])) {
    $errorMSG = "First Name is required ";
} else {
$fname = $_POST["fname"];
}

// LAST NAME
if (empty($_POST["lname"])) {
    $errorMSG = "Last Name is required ";
} else {
    $lname = $_POST["lname"];
}

// EMAIL
if (empty($_POST["email"])) {
    $errorMSG .= "Email is required ";
} else {
    $email = $_POST["email"];
}

// PHONE
if (empty($_POST["phone"])) {
    $errorMSG .= "Phone Number is required ";
} else {
    $phone = $_POST["phone"];
}

// SERVICE
if (empty($_POST["service"])) {
    $errorMSG .= "Service Choice is required ";
} else {
    $service = $_POST["service"];
}

// MESSAGE
if (empty($_POST["message"])) {
    $errorMSG .= "Message is required ";
} else {
    $message = $_POST["message"];
}


$EmailTo = "myemail@gmail.com";
$Subject = "New Message Received";

// prepare email body text
$Body .= "First Name: ";
$Body .= $fname;
$Body .= "\n";
$Body .= "Last Name: ";
$Body .= $lname;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $phone;
$Body .= "\n";
$Body .= "Service: ";
$Body .= $service;
$Body .= "\n";
$Body .= "Message ";
$Body .= "\n";
$Body .= $message;

// send email
$success = mail($EmailTo, $Subject, $Body, "From:".$email);

// redirect to success page
if ($success && $errorMSG == ""){
   echo "success";
}else{
    if($errorMSG == ""){
        echo "Something went wrong :(";
    } else {
        echo $errorMSG;
    }
}

?>

JAVASCRIPT

 $("#contactForm").validator().on("submit", function (event) {
if (event.isDefaultPrevented()) {
    // handle the invalid form...
    formError();
    submitMSG(false, "Did you fill in the form properly?");
} else {
    // everything looks good!
    event.preventDefault();
    submitForm();
     }
 });

 function submitForm(){
     // Initiate Variables With Form Content
     var fname = $("#fname").val();
     var lname = $("#lname").val();
     var email = $("#email").val();
     var phone = $("#phone").val();
     var service = $("#service").val();
     var message = $("#message").val();

     $.ajax({
         type: "POST",
         url: "php/form-process.php",
         data: "fname=" + fname + "&lname=" + lname + "&phone=" + phone  + "&email=" + email + "&service=" + service + "&message=" + message,
         success : function(text){
             if (text == "success"){
                formSuccess();
             } else {
                 formError();
                 submitMSG(false,text);
             }
         }
     });
 }

 function formSuccess(){
     $("#contactForm")[0].reset();
     submitMSG(true, "Message Submitted!")
 }

 function formError(){
 $("#contactForm").removeClass().addClass('shake animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
         $(this).removeClass();
     });
 }

 function submitMSG(valid, msg){
     if(valid){
         var msgClasses = "h3 text-center tada animated text-success";
     } else {
         var msgClasses = "h3 text-center text-danger";
     }
     $("#msgSubmit").removeClass().addClass(msgClasses).text(msg);
 }

任何可以提供帮助的人都会很棒,这是我需要完成的网站的最后一部分。

谢谢你!

1 个答案:

答案 0 :(得分:1)

随着输出的出现,“根据您的代码,出现了“出事了:(”

$success = mail($EmailTo, $Subject, $Body, "From:".$email);

// redirect to success page
if ($success && $errorMSG == ""){
   echo "success";
}else{
    if($errorMSG == ""){
        echo "Something went wrong :(";
    } else {
        echo $errorMSG;
    }
}

邮件尚未发送,并且您在$成功中(即发送邮件时)出错。还可以改善else条件:

if(!empty($errorMSG)){
     echo "Something went wrong, error: " . $errorMSG;
} else {
     echo "Mail not sent";
}