简体   繁体   English

Ajax / PHP联系表格问题

[英]Ajax/PHP contact form issue

I'm building an ajax/php contact form for my project with the following fields: 我正在使用以下字段为我的项目构建一个ajax / php联系表单:

Name (required), Email (required), Subject (Not required) and Website (Not required) 名称(必填),电子邮件(必填),主题(不需要)和网站(不需要)

Everything's working fine the only problem now is if the user doesn't type anything in Subject and or Website fields the email I receive shows those 2 fields like so: 现在一切正常,唯一的问题是,如果用户未在“ 主题”和/或“ 网站”字段中输入任何内容,我收到的电子邮件将显示以下两个字段:

Subject: (shows blank) 主题:(显示为空白)

Website: (shows blank) 网站:(显示为空白)

Is it possible not to show those 2 fields at all if the user didn't type anything so on the email I receive I only get: 如果用户没有输入任何内容,是否可能根本不显示这两个字段,所以在我收到的电子邮件中我只会得到:

Name: [user name] 名称:[用户名]

Email: [user email address] 电子邮件:[用户电子邮件地址]

I'm just posting the PHP code as I believe it's something to do with PHP only and not the Ajax script: 我只是发布PHP代码,因为我认为这仅与PHP有关,与Ajax脚本无关:

<?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"];
}

// SUBJECT
$subject = $_POST["subject"];

// WEBSITE
$website = $_POST["website"];

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


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

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $email;
$Body .= "\n";
$Body .= "Website: ";
$Body .= $website;
$Body .= "\n";
$Body .= "Subject: ";
$Body .= $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;
    }
}

?>

Thank you all! 谢谢你们!

You can check for conditions with an if statement. 您可以使用if语句检查条件。 Something like this: 像这样:

if (!empty($subject)) {
    $Body .= "Subject: ";
    $Body .= $subject;
    $Body .= "\n";
}

You don't use formData , so disabling the field won't work. 您不使用formData ,因此disabling该字段将无效。

If you want to get rid of empty variables, build the data according to a condition like this 如果要摆脱空变量,请按照以下条件构建数据

function submitForm() {

    var data = {
        name: $("#name").val(),
        email: $("#email").val()
    }

    if($("#subject").val() !== "") {
        data.subject = $("#subject").val()
    }

    if($("#website").val() !== "") {
        data.website = $("#website").val()
    }

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

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

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