mrrosscrawford
mrrosscrawford

Reputation: 1

how to make form fields mandatory in php

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 = "[email protected], [email protected], [email protected]";
}
else if ($contact_type=="2" || $contact_type=="3") 
{
    $form_desc = "Call-back Request Submitted";
    $to_email = "[email protected], [email protected], [email protected]";
}
else if ($contact_type=="4") 
{
    $form_desc = "Training Seminar Registration";
    $to_email = "[email protected]";
}


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


}
?>

Upvotes: 0

Views: 5183

Answers (3)

Pavel Zdarov
Pavel Zdarov

Reputation: 2347

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
}

Upvotes: 1

cwnOnTheHill
cwnOnTheHill

Reputation: 29

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

Upvotes: 0

Jack Miller
Jack Miller

Reputation: 7707

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)

Upvotes: 0

Related Questions