Ajax / PHP联系表单空白字段

时间:2016-03-03 04:35:56

标签: javascript php jquery html ajax

您好我已经在我的网站上发了一个联系表格但是当用户输入详细信息并提交表格时,它会发送一些空白信息,但找不到原因。此外,一个主要问题,表单更改了国家/地区的名称字段,因此它的名称如“姓名:法国”而不是“姓名:约翰”。

我还从这里https://github.com/1000hz/bootstrap-validator添加了表单验证器。任何人都知道为什么会这样吗? 以下是js,php和html。

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


function submitForm(){
    // Initiate Variables With Form Content
    var name = $("#name").val();
	var last = $("#last").val();
    var email = $("#email").val();
	var company = $("#company").val();
	var users = $("#users").val();
	var country = $("#country").val();
    var msg_subject = $("#msg_subject").val();
    var message = $("#message").val();


    $.ajax({
        type: "POST",
        url: "assets/php/form-process.php",
        data: "name=" + name + "&last=" + last + "&email=" + email + "&company=" + company + "&users=" + users + "&country=" + country + "&msg_subject=" + msg_subject + "&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);
}
<?php

$errorMSG = "";

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

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

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

// COMPANY
if (empty($_POST["company"])) {
    $errorMSG = "Company name is required ";
} else {
    $name = $_POST["company"];
}

// USERS
if (empty($_POST["users"])) {
    $errorMSG = "Users number is required ";
} else {
    $name = $_POST["users"];
}

// COUNTRY
if (empty($_POST["country"])) {
    $errorMSG = "Country is required ";
} else {
    $name = $_POST["country"];
}

// MSG SUBJECT
if (empty($_POST["msg_subject"])) {
    $errorMSG .= "Subject is required ";
} else {
    $msg_subject = $_POST["msg_subject"];
}


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


$EmailTo = "info@email.com";
$Subject = "New Message Received";

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Last: ";
$Body .= $last;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $company;
$Body .= "\n";
$Body .= "Users: ";
$Body .= $users;
$Body .= "\n";
$Body .= "Country: ";
$Body .= $country;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= $msg_subject;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $message;
$Body .= "\n";

// 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;
    }
}

?>
<html>		
<form id="contactForm" class="contact-form">
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label class="sr-only">First name</label>
                <input type="text" name="name" id="name" class="form-control" placeholder="First Name" required data-error="Please enter your First name">
                    <div class="help-block with-errors"></div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <label class="sr-only">Last name</label>
                    <input type="text" name="last" id="last" class="form-control" placeholder="Last Name" required data-error="Please enter your Last name">
                        <div class="help-block with-errors"></div>
                    </div>
                </div>
            </div>
            <!-- end of /.row -->
            <div class="col-md-12">
                <div class="form-group">
                    <label class="sr-only">Email address</label>
                    <input type="email" name="email" id="email" class="form-control" placeholder="Your Email" required data-error="Please enter your Email address">
                        <div class="help-block with-errors"></div>
                    </div>
                </div>
                <div class="col-md-12">
                    <div class="form-group">
                        <label class="sr-only">Company</label>
                        <input type="text" name="company" id="company" class="form-control" placeholder="Company" required data-error="Please enter your Company name">
                            <div class="help-block with-errors"></div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="Users" class="sr-only legacy-label">Users</label>
                                <select class="form-control" id="users" input name="users" required data-error="Please select the User Number">
                                    <option value="">Users Number</option>
                                    <option value="1 - 3">1 - 3</option>
                                    <option value="4 - 7">4 - 7</option>
                                    <option value="8 - 12">8 - 12</option>
                                    <option value="More than 13">More than 13</option>
                                </select>
                                <div class="help-block with-errors"></div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="Country" class="sr-only legacy-label">Country</label>
                                <select class="form-control" id="country" input name="country" required data-error="Please select your Country">
                                    <option value="">Select your country</option>
                                    <option value="Albania">Albania</option>
                                    <option value="Algeria">Algeria</option>
                                    <option value="American Samoa">American Samoa</option>
                                    <option value="Andorra">Andorra</option>
                                    <option value="And so on...">And so on...</option>
                                </select>
                                <div class="help-block with-errors"></div>
                            </div>
                        </div>
                    </div>
                    <!-- end of /.row -->
                    <div class="col-md-12">
                        <div class="form-group">
                            <label class="sr-only">Subject</label>
                            <input type="text" name="msg_subject" id="msg_subject" class="form-control" placeholder="Subject" required data-error="Please enter your Subject">
                                <div class="help-block with-errors"></div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="sr-only">Your Message</label>
                                <textarea name="message" id="message" class="form-control" rows="5" placeholder="Your Message" required data-error="Please enter your Message"></textarea>
                                <div class="help-block with-errors"></div>
                            </div>
                        </div>
                        <button type="submit" id="submit" class="btn btn-base-alt btn-cmd">Send Message</button>
                    </form>
					</html>

非常感谢任何帮助:)

1 个答案:

答案 0 :(得分:0)

你的一个问题是变量归因,每个输入都应该有自己的变量

你做

$name = $_POST["name"];
...
$last = $_POST["last"];
...
$company = $_POST["company"];
...
$users = $_POST["users"];
...
$country = $_POST["country"];

你应该

{{1}}

最后归因是国家/地区,为什么你有

  

姓名:法国&#34;而不是&#34;姓名:John&#34;