简体   繁体   English

联系我们表单没有将所有填充的字段发送到电子邮件,只有几个? PHP AJAX JS

[英]Contact us form isn't sending all populated fields to email, only a few? PHP AJAX JS

So I've gone through a thread that helped me build this contact us form, but it talked about validating and making the fields required.所以我浏览了一个帮助我构建此联系我们表单的线程,但它谈到了验证和制作所需的字段。 There are two fields I have in my form that doesn't need to be required $organization and $budget , I can't seem to get the information in there to send with the email.我的表单中有两个字段不需要$organization$budget ,我似乎无法在其中获取信息以通过电子邮件发送。 The email shows those fields as blank but the other fields are populated.电子邮件将这些字段显示为空白,但其他字段已填充。

I have practically zero experience in PHP outside of this form.除了这种形式之外,我在 PHP 方面的经验几乎为零。

Expected output:预期输出:

Name: xxx
Email: xxx@xxx.com
Organization: xxx
Budget: xxx
Message: xxx

What I actually receive in my email:我在电子邮件中实际收到的内容:

Name: xxx
Email: xxx@xxx.com
Organization:
Budget:
Message: xxx

Code:代码:

<?php

$errorMSG = "";

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

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

// Organization
    $organization = $_POST["organization"];


// Budget
    $budget = $_POST["budget"];


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


$EmailTo = "example@mydomain.com";
$Subject = "Received msg from WIP contact form";

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Organization: ";
$Body .= $organization;
$Body .= "\n";
$Body .= "Budget: ";
$Body .= $budget;
$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 "Sorry, something went wrong.";
    } else {
        echo $errorMSG;
    }
}

?>

HTML: HTML:

<form role="form" id="contactForm" data-toggle="validator" class="shake">
    <div class="row">
        <div class="form-group col-sm-6">
            <label for="name" class="h4">Name</label>
            <input type="text" class="form-control" id="name" placeholder="Enter name" required>
            <div class="help-block with-errors"></div>
        </div>
        <div class="form-group col-sm-6">
            <label for="email" class="h4">Email</label>
            <input type="email" class="form-control" id="email" placeholder="Enter email" required>
            <div class="help-block with-errors"></div>
        </div>
        <div class="form-group col-sm-6">
            <label for="organization" class="h4">Organization</label>
            <input type="text" class="form-control" id="organization" placeholder="Enter organization">
            <div class="help-block with-errors"></div>
        </div>
        <div class="form group col-sm-6">
            <label for="budget" class="h4">Budget</label>
            <input type="number" class="form-control" id="budget" placeholder="$50000">
            <div class="help-block with-errors"></div>
        </div>
    </div>
    <div class="form-group">
        <label for="message" class="h4">Message</label>
        <textarea id="message" class="form-control" rows="5" placeholder="Enter your message" required></textarea>
        <div class="help-block with-errors"></div>
    </div>
    <button type="submit" id="form-submit" class="button pull-right ">Submit</button>
    <div id="msgSubmit" class="h3 text-center hidden"></div>
    <div class="clearfix"></div>
</form>

JavaScript: JavaScript:

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


function submitForm(){
    // Initiate Variables With Form Content
    var name = $("#name").val();
    var email = $("#email").val();
    var message = $("#message").val();
    var organization = $("#organization").val();
    var budget = $("#budget").val();

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


function formSuccess(){
    $("#contactForm")[0].reset();
    submitMSG(true, "We'll be in touch with you soon!")
}

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";
    } else {
        var msgClasses = "h3 text-center text-danger";
    }
    $("#msgSubmit").removeClass().addClass(msgClasses).text(msg);
}

EDIT编辑

Thanks to Uxmal for suggesting the name="xxxx" in the input field.感谢 Uxmal 在输入字段中建议name="xxxx" He said that by doing this, the $_POST is looking for inputs with name .他说通过这样做, $_POST正在寻找具有name输入。 Which made me realise I must be doing something else wrong.这让我意识到我一定做错了什么。 Turns out my ajax was poorly written and I was also missing the <form action aswell.原来我的 ajax 写得不好,我也错过了<form action Below is the corrected version of the form and ajax code.以下是表单和ajax代码的更正版本。

<form action="form-process.php" method="POST" role="form" id="contactForm" data-toggle="validator" class="shake">
    <div class="row">
        <div class="form-group col-sm-6">
            <label for="name" class="h4">Name</label>
            <input type="text" class="form-control" id="name" name="name" placeholder="Enter name" required>
            <div class="help-block with-errors"></div>
        </div>
        <div class="form-group col-sm-6">
            <label for="email" class="h4">Email</label>
            <input type="email" class="form-control" id="email" name="email" placeholder="Enter email" required>
            <div class="help-block with-errors"></div>
        </div>
    </div>
    <div class="row">
        <div class="form-group col-sm-6">
            <label for="organization" class="h4">Organization</label>
            <input type="text" class="form-control" id="organization" name="organization" placeholder="Enter organization name">
            <div class="help-block with-errors"></div>
        </div>
        <div class="form group col-sm-6">
            <label for="budget" class="h4">Budget</label>
            <input type="number" class="form-control" id="budget" name="budget" placeholder="50000">
            <div class="help-block with-errors"></div>
        </div>
    </div>
    <div class="form-group">
        <label for="message" class="h4">Message</label>
        <textarea id="message" class="form-control" rows="5" name="message" placeholder="Enter your message" required></textarea>
        <div class="help-block with-errors"></div>
    </div>
    <button type="submit" id="form-submit" class="button pull-right ">Submit</button>
    <div id="msgSubmit" class="h3 text-center hidden"></div>
    <div class="clearfix"></div>
</form>

JS: JS:

function submitForm(){

$.ajax({
    type: "POST",
    url: "php/form-process.php",
    data: $('#contactForm').serialize(),
    success: function(text) {
        if (text == "success") {
            formSuccess();
        } else {
            formError();
            submitMSG(false, text);
        }
    }
});
}

You PHP code at line你在线的 PHP 代码

// Organization
$organization = $_POST["organization"];
// Budget
$budget = $_POST["budget"];

looks for inputs with name : organization and budget, no to for id organization and budget.查找名称为:组织和预算的输入,对于id组织和预算为 no。

At you html is在你 html 是

<input type="text" class="form-control" id="organization" placeholder="Enter organization">
<input type="number" class="form-control" id="budget" placeholder="$50000">

without a tag: name="organization" and tag: name="budget".没有标签:名称=“组织”和标签:名称=“预算”。

So put those tags like所以把这些标签像

<input type="text" class="form-control" id="organization" name="organization" placeholder="Enter organization">
<input type="number" class="form-control" id="budget" name="budget" placeholder="$50000">

and then your PHP code will get the data.然后您的 PHP 代码将获取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM