如何在PHP联系表单中创建列表是强制性的

时间:2014-06-15 11:32:53

标签: php form-submit

我有一个现有的联系表单,使用下面的PHP文件发送电子邮件。

我需要列出' market_segment'强制性的。如何修改此文件,以便至少选择一个选项,否则会显示错误?

非常感谢!

<?php
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "email@email.com";
    $email_subject = "Subject";


    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form your submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['title']) ||
        !isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['market_segment']) ||
        !isset($_POST['company_name']) ||
        !isset($_POST['business_address1']) ||
        !isset($_POST['business_address2']) ||
        !isset($_POST['city']) ||
        !isset($_POST['post_code']) ||
        !isset($_POST['where']) ||
        !isset($_POST['code'])) {
        died('We are sorry, but there appears to be a problem with the form your submitted.');      
    }

    $one = $_POST['one']; // not required
    $two = $_POST['two']; // not required
    $three = $_POST['three']; // not required
    $four = $_POST['four']; // not required
    $five = $_POST['five']; // not required
    $title = $_POST['title']; // not required
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // required
    $market_segment = $_POST['market_segment']; // required
    $company_name = $_POST['company_name']; // required
    $business_address1 = $_POST['business_address1']; // required
    $business_address2 = $_POST['business_address2']; // required
    $city = $_POST['city']; // required
    $post_code = $_POST['post_code']; // required
    $current_yes = $_POST['current_yes']; // not required
    $current_no = $_POST['current_no']; // not required
    $future_yes = $_POST['future_yes']; // not required
    $future_no = $_POST['future_no']; // not required
    $where = $_POST['where']; // not required
    $code = $_POST['code']; // required

    $error_message = "";
    $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
  if(!eregi($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!eregi($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  $string_exp = "^[a-z0-9 .'-]+$";
  if(!eregi($string_exp,$company_name)) {
    $error_message .= 'The Company Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 0) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  $string_exp = "^[0-9 .-]+$";
  if(!eregi($string_exp,$telephone)) {
    $error_message .= 'The Telephone Number you entered does not appear to be valid.<br />';
      }
  $string_exp = "^7575$";
  if(!eregi($string_exp,$code)) {
    $error_message .= 'The Verification Code you entered does not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "President Creme Brulee with Vanilla Pods: ".clean_string($one)."\n";
    $email_message .= "President Creme Brulee with Salted Butter Caramel: ".clean_string($two)."\n";
    $email_message .= "President Cheesecake: ".clean_string($three)."\n";
    $email_message .= "Galbani Panna Cotta: ".clean_string($four)."\n";
    $email_message .= "Galbani Tiramisu: ".clean_string($five)."\n";
    $email_message .= "Title: ".clean_string($title)."\n";
    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Market Segment: ".clean_string($market_segment)."\n";
    $email_message .= "Company Name: ".clean_string($company_name)."\n";
    $email_message .= "Business Address 1: ".clean_string($business_address1)."\n";
    $email_message .= "Business Address 2: ".clean_string($business_address2)."\n";
    $email_message .= "City: ".clean_string($city)."\n";
    $email_message .= "Post Code: ".clean_string($post_code)."\n";
    $email_message .= "Current Customer: ".clean_string($current_yes)."\n";
    $email_message .= "Existing Customer: ".clean_string($current_no)."\n";
    $email_message .= "Future Marketing Accepted: ".clean_string($future_yes)."\n";
    $email_message .= "Future Marketing Declined: ".clean_string($future_no)."\n";
    $email_message .= "Where: ".clean_string($where)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->
<link href="example.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body {
    background-color: #FFF;
}
-->
</style>

yes

<?
}
?>

1 个答案:

答案 0 :(得分:0)

您可以使用此功能检查变量是否为空:

bool empty ( mixed $var )

如果它是空的,则返回true,否则,在你的情况下:

// validation expected data exists
if(empty($_POST['first_name']) ||
    empty($_POST['last_name']) ||
    empty( $_POST['email']) ||
    empty($_POST['telephone']) ||
    empty($_POST['market_segment']) ||
    empty($_POST['company_name']) ||
    empty($_POST['company_name']) ||
    empty($_POST['business_address1']) ||
    empty($_POST['business_address2']) ||
    empty($_POST['city']) ||
    empty($_POST['post_code'])) {
    died('We are sorry, but there appears to be a problem with the form your submitted.');      
}