仅包含在电子邮件中回答的表单问题

时间:2016-12-08 07:32:21

标签: javascript php forms email html-email

因此,我正在为非盈利组织创建一个网站,他们想要的一件事就是通过电子邮件发送出来的志愿者表格。但是,我想只包含电子邮件中回答的问题。如果没有答案,我不会发现需要向他们发送字段。目前我有复选框给我一个真或假,并说他们是否对该部分感兴趣。但是,如果说他们没有检查任何东西,我不希望他们得到一个错误的"对于某些事情,我只想获得他们回答的信息。另外,如果没有填写下拉选项,我也不想让我发送空白插槽。

这是光荣的代码:

    <?php
// Check for empty fields
if(empty($_POST['name'])      ||
   empty($_POST['lastName'])      ||
   empty($_POST['gender'])      ||
   empty($_POST['email'])     ||
   empty($_POST['phone'])     ||
   empty($_POST['address'])     ||
   empty($_POST['city'])     ||
   empty($_POST['state'])     ||
   empty($_POST['country'])     ||
   empty($_POST['zipcode'])     ||
   empty($_POST['message'])   ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
   echo "No arguments Provided!";
   return false;
   }

$name = strip_tags(htmlspecialchars($_POST['name']));
$lastName = strip_tags(htmlspecialchars($_POST['lastName']));
$gender = strip_tags(htmlspecialchars($_POST['gender']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$address= strip_tags(htmlspecialchars($_POST['address']));
$city= strip_tags(htmlspecialchars($_POST['city']));
$state= $_POST['state'];
$country= $_POST['country'];
$zipcode= strip_tags(htmlspecialchars($_POST['zipcode']));
$Title= $_POST['Title'];
$otherTitle = $_POST['otherTitle'];
$specialty= $_POST['specialty'];
$otherSpecialty= $_POST['otherSpecialty'];
$skill= $_POST['skill'];
$otherSkill= $_POST['otherSkill'];
$africa= $_POST['africa'];
$caribbean= $_POST['caribbean'];
$eSeAsia= $_POST['eSeAsia'];
$europe= $_POST['europe'];
$india= $_POST['india'];
$latinAmerica= $_POST['latinAmerica'];
$arabic= $_POST['arabic'];
$arabiclevel= $_POST['arabicLevel'];
$chinese= $_POST['chinese'];
$chineselevel= $_POST['chineseLevel'];
$english= $_POST['english'];
$englishlevel= $_POST['englishLevel'];
$french= $_POST['french'];
$frenchlevel= $_POST['frenchLevel'];
$german= $_POST['german'];
$germanlevel= $_POST['germanLevel'];
$hindi= $_POST['hindi'];
$hindilevel= $_POST['hindiLevel'];
$korean= $_POST['korean'];
$koreanlevel= $_POST['koreanLevel'];
$lao= $_POST['lao'];
$laolevel= $_POST['laoLevel'];
$portuguese= $_POST['portuguese'];
$portugueselevel= $_POST['portugueseLevel'];
$spanish= $_POST['spanish'];
$spanishlevel= $_POST['spanishLevel'];
$vietnamese= $_POST['vietnamese'];
$vietnameselevel= $_POST['vietnameseLevel'];
$other= $_POST['other'];
$otherlevel= $_POST['otherLevel'];
$otherlanguage= strip_tags(htmlspecialchars($_POST['otherInput']));
$volunteerYes= $_POST['volunteerYes'];
$volunteerCountry= strip_tags(htmlspecialchars($_POST['volunteerCountry']));
$volunteerNo= $_POST['volunteerNo'];
$message = strip_tags(htmlspecialchars($_POST['message']));

// Create the email and send the message
$to = 'youngmarines37@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.

$email_subject = "Website Volunteer Form:  $name";
    $email_body = "You have received a new message from
    your website volunteer form.\n\n".
    "Here are the details:\n";


    foreach($_POST as $key => $value) {
        $value = strip_tags(htmlspecialchars($value));
        if($value!="" && $value!="false") {
            $email_body .= "$key : $value \n";
        }
    }


/*
$email_subject = "Website Volunteer Form:  $name";
$email_body = "You have received a new message from
your website volunteer form.\n\n".
"Here are the details:\nFirst Name: $name\nLast Name: $lastName\nGender: $gender\n\nEmail: $email_address\nPhone: $phone\n\n
Address: $address\nCity: $city\nState: $state\nCountry: $country\nZip: $zipcode\n\n
Medical Degree/Title: $Title\nSpecality: $specialty\nNon-Medical Skill: $skill\n\n
Interested in Africa: $africa\nInterested in Caribbean: $caribbean\nInterested in E. or S.E. Asia: $eSeAsia\nInterested in Europe: $europe\nInterested in India: $india\nInterested in Latin America: $latinAmerica\n\n
Message:\n$message";
*/
$headers = "From: noreplyw4wh@gmail.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>

表格JS:

    /*
  Jquery Validation using jqBootstrapValidation
   example is taken from jqBootstrapValidation docs 
  */
$(function() {

    $("#contact_form input,#contact_form textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // something to have when submit produces an error ?
            // Not decided if I need it yet
        },
        submitSuccess: function($form, event) {
            event.preventDefault(); // prevent default submit behaviour
            // get values from FORM
            var name = $("input#name").val();
            var lastName = $("input#lastName").val();
            var gender = $("select#gender").val();
            var phone = $("input#phone").val();
            var email = $("input#email").val();
            var address = $("input#address").val();
            var city = $("input#city").val();
            var state = $("select#state").val();
            var country = $("select#country").val();
            var zip = $("input#zip").val();
            var title = $("select#title").val();
            var otherTitle = $("input#otherTitle").val();
            var specialty = $("select#specialty").val();
            var otherSpecialty = $("input#otherSpecialty").val();
            var skill = $("select#skill").val();
            var otherSkill = $("input#otherSkill").val();
            var africa = document.getElementById("africa").checked;
//            var africa = $("input#africa").checked;
            var caribbean = document.getElementById("caribbean").checked;
//            var caribbean = $("input#caribbean").checked;
            var eSeAsia = document.getElementById("eseAsia").checked;
//            var eSeAsia = $("input#eseAsia").val();
            var europe = document.getElementById("europe").checked;
//            var europe = $("input#europe").val();
            var india = document.getElementById("india").checked;
//            var india = $("input#india").val();
            var latinAmerica = document.getElementById("latinAmerica").checked;
//            var latinAmerica = $("input#latinAmerica").val();
//            var arabic = $("input#arabic").val();
            var arabic = document.getElementById("arabic").checked;
            var arabicLevel = $("select#arabicLevel").val();
//            var chinese = $("input#chinese").val();
            var chinese = document.getElementById("chinese").checked;
            var chineseLevel = $("select#chineseLevel").val();
//            var english = $("input#english").val();
            var english = document.getElementById("english").checked;
            var englishLevel = $("select#englishLevel").val();
//            var french = $("input#french").val();
            var french = document.getElementById("french").checked;
            var frenchLevel = $("select#frenchLevel").val();
//            var german = $("input#german").val();
            var german = document.getElementById("german").checked;
            var germanLevel = $("select#germanLevel").val();
//            var hindi = $("input#hindi").val();
            var hindi = document.getElementById("hindi").checked;
            var hindiLevel = $("select#hindiLevel").val();
//            var korean = $("input#korean").val();
            var korean = document.getElementById("korean").checked;
            var koreanLevel = $("select#koreanLevel").val();
//            var lao = $("input#lao").val();
            var lao = document.getElementById("lao").checked;
            var laoLevel = $("select#laoLevel").val();
//            var portuguese = $("input#portuguese").val();
            var portuguese = document.getElementById("portuguese").checked;
            var portugueseLevel = $("select#portugueseLevel").val();
//            var spanish = $("input#spanish").val();
            var spanish = document.getElementById("spanish").checked;
            var spanishLevel = $("select#spanishLevel").val();
//            var vietnamese = $("input#vietnamese").val();
            var vietnamese = document.getElementById("vietnamese").checked;
            var vietnameseLevel = $("select#vietnameseLevel").val();
//            var other = $("input#other").val();
            var other = document.getElementById("other").checked;
            var otherLevel = $("select#otherLevel").val();
            var otherInput = $("input#otherInput").val();
            var volunteerYes = document.getElementById("volunteerYes").checked;
            var organizationCountry = $("input#volunteerCountry").val();
            var volunteerNo = document.getElementById("volunteerNo").checked;
            var message = $("textarea#message").val();

            var firstName = name; // For Success/Failure Message
            // Check for white space in name for Success/Fail message
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }
            $.ajax({
                url: "./bin/volunteer.php",
                type: "POST",
                data: {
                    name: name,
                    lastName: lastName,
                    gender: gender,
                    phone: phone,
                    email: email,
                    address: address,
                    city: city,
                    state: state,
                    country: country,
                    zipcode: zip,
                    Title: title,
                    otherTitle: otherTitle,
                    specialty: specialty,
                    otherSpecialty: otherSpecialty,
                    skill: skill,
                    otherSkill: otherSkill,
                    africa: africa,
                    caribbean: caribbean,
                    eSeAsia: eSeAsia,
                    europe: europe,
                    india: india,
                    latinAmerica: latinAmerica,
                    arabic: arabic,
                    arabiclevel: arabicLevel,
                    chinese: chinese,
                    chineselevel: chineseLevel,
                    english: english,
                    englishlevel: englishLevel,
                    french: french,
                    frenchlevel: frenchLevel,
                    german: german,
                    germanlevel: germanLevel,
                    hindi: hindi,
                    hindilevel: hindiLevel,
                    korean: korean,
                    koreanlevel: koreanLevel,
                    lao: lao,
                    laolevel: laoLevel,
                    portuguese: portuguese,
                    portugueselevel: portugueseLevel,
                    spanish: spanish,
                    spanishlevel: spanishLevel,
                    vietnamese: vietnamese,
                    vietnameselevel: vietnameseLevel,
                    other: other,
                    otherlevel: otherLevel,
                    otherlanguage: otherInput,
                    volunteerYes: volunteerYes,
                    volunteerCountry: volunteerCountry,
                    volunteerNo: volunteerNo,
                    message: message
                },
                cache: false,
                success: function() {
                    // Success message
                    $('#success').html("<div class='alert alert-success'>");
                    $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-success')
                        .append("<strong>Your message has been sent. </strong>");
                    $('#success > .alert-success')
                        .append('</div>');

                    //clear all fields
                    $('#contact_form').trigger("reset");
                },
                error: function() {
                    // Fail message
                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-danger').append("<strong>Sorry " + firstName + " it seems that my mail server is not responding...</strong> Could you please email me directly to <a href='mailto:me@example.com?Subject=Message_Me from myprogrammingblog.com;>me@example.com</a> ? Sorry for the inconvenience!");
                    $('#success > .alert-danger').append('</div>');
                    //clear all fields
                    $('#contact_form').trigger("reset");
                },
            })
        },
        filter: function() {
            return $(this).is(":visible");
        },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});


/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
    $('#success').html('');
});

发送电子邮件的示例:

  

您收到了网站志愿者表单中的新消息。

     

以下是详细信息:名字:James姓:Singleton性别:

     

电子邮件:testing@gmail.com电话:1234567890

     

地址:3672 A Orange Ave城市:列克星敦州:弗吉尼亚州:美国   邮编:94450

     

医学学位/职称:特殊性:心脏病学非医学技能:基金   雷塞

     

对非洲感兴趣:真正对加勒比海感兴趣:真正感兴趣   E.或S.E.亚洲:false对欧洲感兴趣:真正对印度感兴趣:   false对拉丁美洲感兴趣:是的

     

消息:测试123

1 个答案:

答案 0 :(得分:1)

通过这样做,您只能包含具有某些值且不是false的字段。你必须稍微改变一下你的字段名称才能合并。

    // Create the email and send the message
    $to = 'you@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
    $email_subject = "Website Volunteer Form:  $name";
    $email_body = "You have received a new message from
    your website volunteer form.\n\n".
    "Here are the details:\n";


    foreach($_POST as $key => $value) {
        $value = strip_tags(htmlspecialchars($value));
        if($value!="" && $value!="false") {
            $email_body .= "$key : $value \n";
        }
    }

    //$email_body .="First Name: $name\nLast Name: $lastName\nGender: $gender\n\nEmail: $email_address\nPhone: $phone\n\n
    //Address: $address\nCity: $city\nState: $state\nCountry: $country\nZip: $zipcode\n\n
    //Medical Degree/Title: $Title\nSpecality: $specialty\nNon-Medical Skill: $skill\n\n
    //Interested in Africa: $africa\nInterested in Caribbean: $caribbean\nInterested in E. or S.E. Asia: $eSeAsia\nInterested in Europe: $europe\nInterested in India: $india\nInterested in Latin America: $latinAmerica\n\n
    //Message:\n$message";
    $headers = "From: test@gmail.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
    $headers .= "Reply-To: $email_address";
    mail($to,$email_subject,$email_body,$headers);