0

I have these forms created in PHP but no of them have mandatory fields and I want to make the name, telephone number and email mandatory. How do I do this?

Here is the code:

<?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');


}
?>
Luc M
  • 16,630
  • 26
  • 74
  • 89

3 Answers3

1

Better to use client-side validation (in such case you can warm users about mandatory fields). And from PHP side you can validate like:

if (isset($contact) && isset($tel) && isset($email)) {
    // your code here
}
Pavel Zdarov
  • 2,347
  • 3
  • 15
  • 23
0

You could use client-side validation using JS. Of course it is not PHP, but this saves time for the user. Refer e.g. to: http://www.w3resource.com/javascript/form/non-empty-field.php

Alternatively, if you use jQuery: jQuery: checking if the value of a field is null (empty)

Community
  • 1
  • 1
Jack Miller
  • 6,843
  • 3
  • 48
  • 66
  • +1. Use any Java Script library like jquery for doing this stuff. No need to put this check on server unnecessarily... – Rahul Shelke Jul 03 '13 at 13:07
0

this should help...

// 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';
}
?>