Php联系表格 - 可选字段(修剪)

时间:2012-08-02 02:47:54

标签: php forms trim contact

我有一个php联系表单,我正在尝试编辑以满足我的需求。目前,需要填写每个字段才能使表单生效。为了使某些字段可选,我需要编辑什么?下面不是完整的脚本,但我感觉它对于我需要做的事情最重要的部分。

由于

$name    = $_POST['name'];
$email  = $_POST['email'];
$phone  = $_POST['phone'];
$date    = $_POST['date'];
$guests  = $_POST['guests'];
$subject  = $_POST['subject'];
$comments = $_POST['comments'];

if (isset($_POST['verify'])) : 
    $posted_verify   = $_POST['verify']; 
    $posted_verify   = md5($posted_verify); 
else :
    $posted_verify = '';
endif;

// Important Variables
$session_verify = $_SESSION['verify'];

if (empty($session_verify)) $session_verify = $_COOKIE['verify'];

$error = '';

    if(trim($name) == '') {
        $error .= '<li>Your name is required.</li>';
    }

    if(trim($date) == '') {
        $error .= '<li>Your event date is required.</li>';
    }

    if(trim($email) == '') {
        $error .= '<li>Your e-mail address is required.</li>';
    } elseif(!isEmail($email)) {
        $error .= '<li>You have entered an invalid e-mail address.</li>';
    }

    if(trim($phone) == '') {
        $error .= '<li>Your phone number is required.</li>';
    } elseif(!is_numeric($phone)) {
        $error .= '<li>Your phone number can only contain digits.</li>';
    }


    if(trim($comments) == '') {
        $error .= '<li>You must enter a message to send.</li>';
    }

    if(trim($guests) == '') {
        $error .= '<li>The number of guests is required.</li>';
    } elseif(!is_numeric($guests)) {
        $error .= '<li>Your phone number can only contain digits.</li>';
    }

1 个答案:

答案 0 :(得分:1)

删除您想要选择的变量的if语句。

示例删除此名称是可选的: if(trim($name) == '') { $error .= '<li>Your name is required.</li>'; }