Php联系人表格-可选字段(修饰) [英] Php Contact Form - Optional Fields (trim)

查看:96
本文介绍了Php联系人表格-可选字段(修饰)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php联系人表单,我正在尝试对其进行编辑以适合我的需求。当前,为了填写表格,需要填写每个字段。为了使某些字段可选,我需要编辑什么?以下不是完整的脚本,但是我觉得它是该领域对于我需要做的最重要的部分。

I have a php contact form I'm trying to edit to suit my needs. Currently, every field needs to be filled out in order for the form to work. What do I need to edit in order to make some fields optional? Below isn't the full script, but I have a feeling its the segment of the field most important for what I need to do.

谢谢

$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>';
    }


推荐答案

删除if语句

示例将其删除为可选名称:
if(trim($ name)= ='){
$ error。='< li>您的名字是必需的。< / li>';;
}

example remove this for name to be optional: if(trim($name) == '') { $error .= '<li>Your name is required.</li>'; }

这篇关于Php联系人表格-可选字段(修饰)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆