2015-09-24 111 views
0

我發現了一個可以很好地處理多個文件上傳的表單,並且正在嘗試在某些驗證中工作,我只是想要製作一些現在需要的字段。表單驗證 - 以php格式表示的必填字段

目前一些驗證發生錯誤消息,但是該表單始終張貼無論。我的PHP知識是有限的,它可能看起來混亂,但我將不勝感激,如果有人可以幫助我關於如何修改下面的代碼..

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

// we'll begin by assigning the To address and message subject 
$to="[email protected]"; 
$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 } ?> 
+0

你好,你正在檢查你使用的錯誤條件(!isset($ _ POST ['name']))...而不是使用($ _POST ['name'] ==「」)thi s將確保用戶在字段中輸入一些數據... – Akshay

+1

!(isset($ _ POST ['name']))將只檢查post變量是否存在,哪個實際存在,但它可能不爲空... – Akshay

回答

0

使用if (!$_POST['emailfrom'])如果貼字段存在將只檢查。無論它是否爲空。

相反,你可以使用空:

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

您還可以添加一些額外的檢查,到田間地頭像一個有效的電子郵件支票或PHONENUMBER驗證:

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

if(!isset($_POST['namefrom']) || $_POST['namefrom']==""){ 
    $error[]="Please enter your name"; 
} 
+0

謝謝,對不起,不能將代碼標記得很好 - 我改變 '如果 $錯誤[] = 「名稱是必需」;' 到 '如果(($ _ POST [ 'namefrom']!) !isset($ _ POST ['namefrom'])|| $ _POST ['namefrom'] ==「」){$ errors [] =「請輸入您的名字」;}' 它似乎也是這樣做的 如果我要填寫4個字段中的兩個,提交我碰到下面的信息和表格 錯誤: 請輸入您的郵箱地址 請輸入您的聯繫號碼 消息發送成功,因爲你的病情的其他部分你什麼都不做 –

+0

這就是......在寫你的郵件代碼錯誤檢查的其他部分......也就是說,如果沒有錯誤,則發送郵件,如果出現錯誤,則只需再次顯示錶單以及錯誤... – Akshay