如果选项选择为true,则将其发布,否则显示错误PHP

时间:2014-04-29 00:21:34

标签: javascript php forms

对不起有人问这个,但我无法找到我正在寻找的任何地方......好吧,找不到我正在寻找的......好吧,我有这个联系表格。我已经构建了它,因此它会将表单上的任何内容发送到我的电子邮件中。但我只知道如何使用简单的表单,只适用于字符串。现在我需要添加一个选项列表,当有人选择该选项时,它会通过电子邮件向我发送该值。这是代码:

PHP代码

<?php
$main_email = "samplemail@mail.com";

$c_acc = htmlspecialchars ($_POST["account"]);  ----> Gets string from form
$c_location = htmlspecialchars($_POST["location"]);   ----> Gets string from form
$c_server = htmlspecialchars($_POST["server"]);  ----> This is supposed to be the options form

$error = 0;

if(strlen($c_acc) < 1){  ----> If string length is less than 1, it will give me the error
    $error++;
}

if(strlen($c_location) < 1){  ----> If string length is less than 1, it will give me the error
    $error++;
}


if(strlen($c_server) < 1){ ----> I tried doing with string, but it is supposed to check if the option was selected, and give me the error if no option was selecet
    $error++;
}

这是我在html页面上使用的表单:

<form class="contactform" action="sendmail.php" method="post">
    <p class="form_info">Account<span class="needed">*</span></p>
    <input id="c_acc" type="text" name="account" />

    <p class="form_info">Location <span class="needed">*</span></p>
    <input id="c_location" type="text" name="password" />

    <p class="form_info">Server <span class="needed">*</span></p>
    <select id="c_server" name="server">
        <option value="NNY">NNY</option>
        <option value="SNY">SNY</option>
        <option value="NJ">NJ</option>
        <option value="CA">CA</option>
        <option value="TX">TX</option>
    </select>

最后。这是JS文件

function validate_contactform(){

    $('.contactform').append('<div id="c_loader"></div>');
    $('#c_submit').hide();

    var c_acc = $('#c_acc'), c_location = $('#c_location'), c_server = $('#c_server'),

    var filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    var error_exist = 0;

    if(c_acc.val().length < 1){
        c_acc.addClass('error');
        error_exist++;
    }else{
        c_acc.removeClass('error');
    }   

    if(c_location.val().length < 1){
        c_location.addClass('error');
        error_exist++;
    }else{
        c_location.removeClass('error');
    }

    if(c_server.val().length < 1){
        c_server.addClass('error');
        error_exist++;
    }else{
        c_server.removeClass('error');
    }

    if(error_exist > 0){
        $('#c_loader').remove();
        $('#c_submit').show();

    }else{
        var dataString = 'acc='+ c_acc.val() + 'location='+ c_location.val() + 'server='+ c_server.val();

        $.ajax({
            type: "POST",
            url: "sendmail.php",
            data: dataString,
            success: function(response) {
                if(response == "sent"){
                    $('#c_loader').remove();
                    $('.contactform').prepend('<div class="noti_success">Message has been sent successfull! We will get in contact with you shortly</div>');
                }else{
                    $('#c_loader').remove();
                    $('#c_submit').show();
                    $('.contactform').prepend('<div class="noti_error">There was an error while sending your message</div>');
                }

好的,再次抱歉长篇文章...我不知道要改变什么或添加所以它会发送选项的值...正如你所看到的,表单只能发送字符串..就像,只有在表格上输入的内容......没有被选中...请帮助,我不知道现在该做什么......

0 个答案:

没有答案