复选框验证和发布PHP,快速:)

时间:2012-04-26 07:31:54

标签: php validation required

我需要检查复选框是否已选中(必填)并且需要验证才能像其他表单字段一样执行,然后我需要在电子邮件中发布该值。

PHP表示整个表格:

<?php
if(isset($_POST['formtrigger'])):

    //config
    define('FORM_SENDTO','hello@domain.com');
    define('FORM_SUBJECTLINE','Enquiry from website');
    define('ERR_MSG_FIELD_REQUIRED','This field is required.');
    define('ERR_MSG_FIELD_INVALIDEMAIL','Please enter a valid email address.');

    //setup validation rules

        //Name
        $validation_rules['forename']['required'] = true;
        $validation_rules['surname']['required'] = true;

        //Company
        $validation_rules['company']['required'] = true;

        //Address
        $validation_rules['address']['required'] = true;

        //Tel
        $validation_rules['tel']['required'] = true;

        //Email
        $validation_rules['email']['required'] = true;
        $validation_rules['email']['valid_email'] = true;

        //Enquiry
        $validation_rules['enquiry']['required'] = true;

        //title/gender
        $validation_rules['ts1']['required'] = true;

        $validation_rules['ts2']['required'] = true;

    //validate the form
    $formerrors=0;
    foreach($_POST as $formfield_name=>$formfield_value):

        //set the entered value as a sanitised string
        $_POST_SANITISED[$formfield_name] = filter_var($formfield_value,FILTER_SANITIZE_STRING);

        //Check if required
        if($validation_rules[$formfield_name]['required']):
            if(!strlen($formfield_value)>0):
                $formerrors++;
                $fielderrors[$formfield_name][] = ERR_MSG_FIELD_REQUIRED;
            endif;
        endif;

        //Check if valid email required
        if($validation_rules[$formfield_name]['valid_email']):
            if(!filter_var($formfield_value,FILTER_VALIDATE_EMAIL)):
                $formerrors++;
                $fielderrors[$formfield_name][] = ERR_MSG_FIELD_INVALIDEMAIL;
            endif;
        endif;

    endforeach;

    //process form and send message if validation passed
    if($formerrors==0):
        $email_msg[] = "New general enquiry\n\n-----------\n";
        $email_msg[] = "Title: ".$_POST_SANITISED['ts1']."\n";
        $email_msg[] = "Gender: ".$_POST_SANITISED['ts2']."\n";
        $email_msg[] = "Forename: ".$_POST_SANITISED['forename']."\n";
        $email_msg[] = "Surname: ".$_POST_SANITISED['surname']."\n";
        $email_msg[] = "Company: ".$_POST_SANITISED['company']."\n";
        $email_msg[] = "Address: ".$_POST_SANITISED['address']."\n";
        $email_msg[] = "Telephone No.: ".$_POST_SANITISED['tel']."\n";
        $email_msg[] = "Email: ".$_POST_SANITISED['email']."\n";
        $email_msg[] = "Enquiry: ".$_POST_SANITISED['enquiry']."\n";

        $email_msg[] = "-----------\n";

        $email_msg = implode('',$email_msg);

        $email_headers = 'From: ' . $_POST_SANITISED['email'] . "\r\n" .
        'Reply-To: ' . $_POST_SANITISED['email'] . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

        mail(FORM_SENDTO,FORM_SUBJECTLINE,$email_msg,$email_headers);

        header('Location: ?msgsent=1#thanks');

    endif;

endif;

function errorOutput($fieldname=''){
    global $fielderrors;
    if(count($fielderrors[$fieldname])>0):
        foreach($fielderrors[$fieldname] as $err_msg):
            $error_str .= '<div class="form-fielderror-msg">'.$err_msg.'</div>';
        endforeach;
    endif;
    return $error_str ? $error_str : false;
}

function errorClass($fieldname=''){
    global $fielderrors;
    $error_class = '';
    if(count($fielderrors[$fieldname])>0):
       $error_class = 'form-fielderror';
    endif;
    return $error_class ? $error_class : false;
}

&GT;

这是HTML:

        <?php if($_GET['msgsent']==1): ?>

    <h1>Thanks for your enquiry. If requested, we will get in touch shortly.</h1>

    <?php else: ?>


    <div id="form-cont">

        <form action="<?=$_SERVER['PHP_SELF']?>" method="post">



            <div class="form-element">
                <label for="ts1">Title:*</label>
                <select name="ts1" class="<?=errorClass('ts1')?>">
                        <option value="">-- Please select --</option>
                        <option value="Mr" <?=$_POST['ts1']=='Mr' ? 'selected="selected"' : '' ?>>Mr</option>
                        <option value="Mrs" <?=$_POST['ts1']=='Mrs' ? 'selected="selected"' : '' ?>>Mrs</option>
                        <option value="Miss" <?=$_POST['ts1']=='Miss' ? 'selected="selected"' : '' ?>>Miss</option>
                        <option value="Ms" <?=$_POST['ts1']=='Ms' ? 'selected="selected"' : '' ?>>Ms</option>
                </select>
                <?=errorOutput('ts1')?>
            </div>

            <div class="form-element">
                <label for="ts2">Gender:*</label>
                <select name="ts2" class="<?=errorClass('ts2')?>">
                        <option value="">-- Please select --</option>
                        <option value="Male" <?=$_POST['ts2']=='Male' ? 'selected="selected"' : '' ?>>Male</option>
                        <option value="Female" <?=$_POST['ts2']=='Female' ? 'selected="selected"' : '' ?>>Female</option>
                </select>
                <?=errorOutput('ts2')?>
            </div>      

            <div class="form-element">
                <label for="forename">Forename:*</label>
                <input type="text" name="forename" class="textbox <?=errorClass('forename')?>" value="<?=$_POST['forename']?>" />
                <?=errorOutput('forename')?>
            </div>

            <div class="form-element">
                <label for="surname">Surname:*</label>
                <input type="text" name="surname" class="textbox <?=errorClass('surname')?>" value="<?=$_POST['surname']?>" />
                <?=errorOutput('surname')?>
            </div>

            <div class="form-element">
                <label for="company">Company:*</label>
                <input type="text" name="company" class="textbox <?=errorClass('company')?>" value="<?=$_POST['company']?>" />
                <?=errorOutput('company')?>
            </div>

            <div class="form-element">
                <label for="address">Address:*</label>
                <input type="text" name="address" class="textbox <?=errorClass('address')?>" value="<?=$_POST['address']?>" />
                <?=errorOutput('address')?>
            </div>

            <div class="form-element">
                <label for="tel">Telephone No:*</label>
                <input type="text" name="tel" class="textbox <?=errorClass('tel')?>" value="<?=$_POST['tel']?>" />
                <?=errorOutput('tel')?>
            </div>

            <div class="form-element">
                <label for="email">Email:*</label>
                <input type="text" name="email" class="textbox <?=errorClass('email')?>" value="<?=$_POST['email']?>" />
                <?=errorOutput('email')?>
            </div>  

            <div class="form-element">
                <label for="enquiry">Your Enquiry:*</label>
                <textarea name="enquiry" class="<?=errorClass('enquiry')?>"><?=$_POST['enquiry']?></textarea>
                <?=errorOutput('enquiry')?>
            </div>      

            <div class="form-element">
                <label for="terms">Terms and Conditions</label>
                <input type="checkbox" name="terms" class="checkbox <?=errorClass('terms')?>" value="<?=$_POST['terms']=1?>" />
                <?=errorOutput('terms')?>
            </div>      

            <div class="form-element">
            <p class="clear">* denotes required field.</p>
            <input type="submit" class="submit" name="submit" value="Send" alt="Submit" title="Submit" />
            </div>
            <input type="hidden" name="formtrigger" value="1" />
        </form>

    </div>
    <?php endif ?>  

底部有问题的复选框:

<div class="form-element">
            <label for="terms">Terms and Conditions</label>
            <input type="checkbox" name="terms" class="checkbox <?=errorClass('terms')?>" value="<?=$_POST['terms']=1?>" />
            <?=errorOutput('terms')?>
        </div> 

非常感谢你的帮助! :)

2 个答案:

答案 0 :(得分:0)

if (isset($_POST['terms']))
{
    // checked
}

答案 1 :(得分:0)

世界上没有办法让这些代码运行验证框的验证是你问题的最小部分

一个。您有很多Notice: Undefined index错误,例如55

B中。这不起作用,期望您的服务器支持短标记''

℃。复制太多

我只会更正您要求的验证错误,但相信我这么多错误

    $formerrors=0;
    $_POST_SANITISED = array();
    foreach($_POST as $formfield_name=>$formfield_value){
        //set the entered value as a sanitised string
        $_POST_SANITISED[$formfield_name] = filter_var($formfield_value,FILTER_SANITIZE_STRING);
        //Check if required
        if(isset($validation_rules[$formfield_name]['required']))
        {
            if(!strlen($formfield_value)>0){
                $formerrors++;
                $fielderrors[$formfield_name][] = ERR_MSG_FIELD_REQUIRED;
            }
        }
        //Check if valid email required
        if(isset($validation_rules[$formfield_name]['valid_email']))
        {
            if(!filter_var($formfield_value,FILTER_VALIDATE_EMAIL)){
                $formerrors++;
                $fielderrors[$formfield_name][] = ERR_MSG_FIELD_INVALIDEMAIL;
            }
        }
    };

    if(!isset($_POST['terms']))
    {
        $formerrors++;
        $fielderrors["terms"][] = ERR_MSG_FIELD_REQUIRED;       
    }