Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中强制使用表单字段_Php_Forms_Field_Required - Fatal编程技术网

如何在php中强制使用表单字段

如何在php中强制使用表单字段,php,forms,field,required,Php,Forms,Field,Required,我用PHP创建了这些表单,但没有一个表单具有必填字段,我想将姓名、电话号码和电子邮件设置为必填字段。我该怎么做 代码如下: <?php $company = $_POST['company']; $contact = $_POST['name']; $tel = $_POST['phone']; $email = $_POST['email']; $comments = $_POST['message']; $contact_type = $_POST['contact_type'];

我用PHP创建了这些表单,但没有一个表单具有必填字段,我想将姓名、电话号码和电子邮件设置为必填字段。我该怎么做

代码如下:

<?php

$company = $_POST['company'];
$contact = $_POST['name'];
$tel = $_POST['phone'];
$email = $_POST['email'];
$comments = $_POST['message'];

$contact_type = $_POST['contact_type'];

if ($contact_type=="1")
{
    $form_desc = "Contact Request Submitted";
    $to_email = "mail@burlingtongroup.net, ross.crawford@burlingtongroup.net, adam.ninnis@burlingtongroup.net";
}
else if ($contact_type=="2" || $contact_type=="3") 
{
    $form_desc = "Call-back Request Submitted";
    $to_email = "mail@burlingtongroup.net, ross.crawford@burlingtongroup.net, adam.ninnis@burlingtongroup.net";
}
else if ($contact_type=="4") 
{
    $form_desc = "Training Seminar Registration";
    $to_email = "adam.wonnacott@burlingtongroup.com";
}


$source_page = $_SERVER['HTTP_REFERER']; 

//If no errors registred, print the success message
if(isset($_POST['Submit']))
{

    global $mailer; 

        if($contact_type=="3")
        {
        $content = $form_desc.",  \n\n".
                   "Company: ".$company."\n".
                   "Contact: ".$contact."\n".
                   "Tel: ".$tel."\n".                                  
                   "Email: ".$email."\n".                  
                   "Message: ".$comments."\n";  
        }
        else if($contact_type=="4")
        {
            $content = $form_desc.",  \n\n".
                       "Company: ".$company."\n".
                       "Contact: ".$contact."\n".                          
                       "Email: ".$email."\n";       

            //echo $content;
            //exit; 
        }
        else
        {
        $content = $form_desc.",  \n\n".
                   "Company: ".$company."\n".
                   "Contact: ".$contact."\n".
                   "Tel: ".$tel."\n".                                  
                   "Email: ".$email."\n".                  
                   "Message: ".$comments."\n";                         
        }




        mail($to_email, $form_desc.":".$company, $content, "From: ".$email );


        header('Location: '.$source_page.'?sent=1');


}
?>

您可以使用JS进行客户端验证。当然不是PHP,但这为用户节省了时间。例如,参考:

或者,如果您使用jQuery:

这将有助于。。。
// Evaluates to true because $var is empty
if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}

// Evaluates as true because $var is set
if (isset($var)) {
    echo '$var is set even though it is empty';
}
?> 

最好使用客户端验证(在这种情况下,您可以让用户了解必填字段)。从PHP方面,您可以进行如下验证:

if (isset($contact) && isset($tel) && isset($email)) {
    // your code here
}

+1.使用任何Java脚本库(如jquery)来完成这项工作。无需在服务器上进行不必要的检查…没有问题如果这解决了您的问题,请向上投票回答,谢谢