表单验证 - php格式的必填字段

时间:2015-09-24 10:01:04

标签: php forms validation

我发现了一个适用于多个文件上传的表单,并且我正在尝试进行一些验证,我现在只是尝试创建一些字段。

目前正在进行一些验证并显示错误消息,但表单总是无论如何发布。我的PHP知识是有限的,它可能看起来很乱,但如果有人可以帮助我如何修改以下代码,我将不胜感激。

<?php
if ($_SERVER['REQUEST_METHOD']=="POST"){

// we'll begin by assigning the To address and message subject
$to="paul@thedesignbank.co.uk";
$subject="Parts Request";

$subject = trim($_POST['namefrom']);
$vehiclereg = trim($_POST['vehiclereg']);
$chassisnumber = trim($_POST['chassisnumber']); 
$contactnumber = trim($_POST['contactnumber']);
$emailfrom = trim($_POST['emailfrom']);
$address = trim($_POST['address']);
$town = trim($_POST['town']);
$postcode = trim($_POST['postcode']);
$comments = trim($_POST['comments']);



 // get the sender's name and email address
 // we'll just plug them a variable to be used later$from = stripslashes($_POST['fromname'])."                                          <".stripslashes($_POST['fromemail']).">";

 // generate a random string to be used as the boundary marker
 $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

 // now we'll build the message headers
$headers = "From: $from\r\n" .
"MIME-Version: 1.0\r\n" .
  "Content-Type: multipart/mixed;\r\n" .
  " boundary=\"{$mime_boundary}\"";

// here, we'll start the message body.
// this is the text that will be displayed
// in the e-mail
 $message.="From: ".$subject."\n";
      $message.="Email: ".$emailfrom."\n";

      $message.="Vehicle Registration: ".$vehiclereg."\n";
      $message.="Chassis Number: ".$chassisnumber."\n";

      $message.="Address: ".$address."\n";
      $message.="Town: ".$town."\n";
      $message.="Postcode: ".$postcode."\n";
      $message.="Contact Number: ".$contactnumber."\n";
      $message.="Comment: ".$comments."\n\n";   


//my validation gives a message but email still posts
$errors = array();
  if (!$_POST['namefrom'])
  // if not, add that error to our array
  $errors[] = "Name is required";
// check to see if a subject was entered
if (!$_POST['emailfrom'])
  // if not, add that error to our array
  $errors[] = "Email Address is required";     
  if (!$_POST['contactnumber'])
  // if not, add that error to our array
  $errors[] = "Contact Number is required";
// check to see if a message was entered
if (!$_POST['vehiclereg'])
  // if not, add that error to our array
  $errors[] = "vehiclereg is required";
// if there are any errors, display them
if (count($errors)>0){
  echo "<strong>ERROR:<br>\n";
  foreach($errors as $err)
    echo "$err<br>\n";
} else {
}


// next, we'll build the invisible portion of the message body
// note that we insert two dashes in front of the MIME boundary 
// when we use it
$message = "This is a multi-part message in MIME format.\n\n" .
  "--{$mime_boundary}\n" .
  "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
  "Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

// now we'll process our uploaded files
foreach($_FILES as $userfile){
  // store the file information to variables for easier access
  $tmp_name = $userfile['tmp_name'];
  $type = $userfile['type'];
  $name = $userfile['name'];
  $size = $userfile['size'];

  // if the upload succeded, the file will exist
  if (file_exists($tmp_name)){

     // check to make sure that it is an uploaded file and not a system file
     if(is_uploaded_file($tmp_name)){

        // open the file for a binary read
        $file = fopen($tmp_name,'rb');

        // read the file content into a variable
        $data = fread($file,filesize($tmp_name));

        // close the file
        fclose($file);

        // now we encode it and split it into acceptable length lines
        $data = chunk_split(base64_encode($data));
     }

     // now we'll insert a boundary to indicate we're starting the attachment
     // we have to specify the content type, file name, and disposition as
     // an attachment, then add the file content.
     // NOTE: we don't set another boundary to indicate that the end of the 
     // file has been reached here. we only want one boundary between each file
     // we'll add the final one after the loop finishes.
     $message .= "--{$mime_boundary}\n" .
        "Content-Type: {$type};\n" .
        " name=\"{$name}\"\n" .
        "Content-Disposition: attachment;\n" .
        " filename=\"{$fileatt_name}\"\n" .
        "Content-Transfer-Encoding: base64\n\n" .
     $data . "\n\n";
  }
  }
    // here's our closing mime boundary that indicates the last of the message
    $message.="--{$mime_boundary}--\n";
    // now we just send the message   
   if (@mail($to, $subject, $message, $headers))   
   echo "Message Sent Successfully";
   else
   echo "Failed to send";
   } else {
   ?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" 
    enctype="multipart/form-data" name="form1">

 <h3>Vehicle Details</h3>

 <p style="margin:17px 0">  <label style=" font-family: Helvetica, Arial, sans-serif;">Vehicle Registration: <span style="color:red">*</span></label>
 <input class="form-input" name="vehiclereg" id="vehiclereg" type="text" tabindex="1" style="width:100%;"/></p>

 <p style="margin:17px 0">  <label style=" font-family: Helvetica, Arial, sans-serif;">VIN/Chassis Number (Last 6 Digits):</label>
 <input class="form-input" name="chassisnumber" id="chassisnumber" type="text" tabindex="1" style="width:100%;"/></p>




      <h3>Customer Details</h3>
      <p style="margin:17px 0">  <label style=" font-family: Helvetica, Arial, sans-serif;">Name: <span style="color:red">*</span></label>
       <input class="form-input" name="namefrom" id="namefrom" type="text" tabindex="1" style="width:100%;"/></p>

 <!--changed these-->
         <p style="margin:17px 0"><label style=" font-family: Helvetica, Arial, sans-serif;">Address:</label>
     <input class="form-input" name="address" id="address" type="text" tabindex="1" style="width:100%;"/></p>

<p style="margin:17px 0">  <label style=" font-family: Helvetica, Arial, sans-serif;">Town:</label>
<input class="form-input" name="town" id="town" type="text" tabindex="1" style="width:100%;"/></p>

<p style="margin:17px 0">  <label style=" font-family: Helvetica, Arial, sans-serif;">Postcode:</label>
<input class="form-input" name="postcode" id="postcode" type="text" tabindex="1" style="width:100%;"/></p>
<!--changed these-->


<p style="margin:17px 0">  <label style=" font-family: Helvetica, Arial, sans-serif;">Email: <span style="color:red">*</span></label>
<input class="form-input" name="emailfrom" id="emailfrom" type="text" tabindex="3" style="width:100%;"/></p>



   <p style="margin:17px 0">  <label style="margin-right: 4px; font-family: Helvetica, Arial, sans-serif;">Contact No: <span style="color:red">*</span></label>
   <input type="text" class="form-input" name="contactnumber" id="contactnumber" rows="2" cols="22" tabindex="6" style="width:100%;" /></p>

<p style="margin:17px 0">  <label style="margin-right: 4px; vertical-align:top; font-family: Helvetica, Arial, sans-serif; ">Brief Description of Request: </label>
<textarea class="form-input" name="comments" id="comments" rows="7" cols="22" tabindex="6" style="height:50px; width:100%;" ></textarea></p>



 <p style="margin:17px 0">
<label style="margin-right: 4px; font-family: Helvetica, Arial, sans-serif;">Vehicle image/doc one:</label>
<input type="file" name="file1">
</p>


<p style="margin:17px 0">
<label style="margin-right: 4px; font-family: Helvetica, Arial, sans-serif;">Vehicle image/doc two:</label>
<input type="file" name="file2">
</p>

<p style="margin:17px 0">
<label style="margin-right: 4px; font-family: Helvetica, Arial, sans-serif;">Vehicle image/doc three:</label>    
<input type="file" name="file3">
</p>

 <div style="clear:both">
     <p> I would like to subscribe to the newsletter
      <input name="subscribe" type="checkbox" value="website_subscribers" name="mailinglist[]" checked="checked" /></p>
      </div>


   <p><input type="submit" name="Submit" value="Submit"></p>
   </form>
   <?php } ?>

2 个答案:

答案 0 :(得分:0)

使用if (!$_POST['emailfrom'])只会检查发布的字段是否存在。无论它是否为空。

相反,您可以使用空:

if (empty($_POST['emailfrom'])) {
  echo 'error'
}

您还可以在字段中添加一些额外的检查,例如有效的电子邮件检查或phonenumber验证器:

if (empty($_POST['emailfrom']) || !isValidEmailCustomFunction($_POST['emailfrom]')) {
   echo 'error'
}

答案 1 :(得分:0)

类似

if(!isset($_POST['namefrom']) || $_POST['namefrom']=="" ){
    $error[]="Please enter your name";
}
相关问题