0
votes

I'm building an ajax/php contact form for my project with the following fields:

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

$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 protected]";
$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!

2
It may have something to do with PHP but not in the code you shared. Try debugging further to locate where the word "blank" is added to empty fields. - marekful
the word blank doesn't show I typed blank meaning the fields are empty. I just edited that part of the question for better understanding ;) - Nesta
Have you tried to set up any validation checks on your input fields? - Tony
Only the Name and Email have validations - Nesta

2 Answers

1
votes

You can check for conditions with an if statement. Something like this:

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

You don't use formData, so disabling the field won't work.

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