2012-05-31 100 views
1

我有一個聯繫表單與現有的驗證碼。我創建了表單並在其中組合了上載功能。問題是當我試圖提交沒有驗證碼的表單時,它沒有顯示js提醒或無效的驗證碼提示,但是當我嘗試附加圖像時,提交沒有驗證碼的表單,它被提交。現在代碼亂七八糟。任何人都可以查看條件聲明,看看你能幫我解決這個問題嗎?我不擅長php。PHP表單條件語句

這是我mail.php

 <?php 
if($_SESSION["captcha"] == $_POST["captcha"]) { 

    //if($_SESSION["captcha"] == $_POST["captcha"]) { 

    // we'll begin by assigning the To address and message subject 
    $to="[email protected]"; 

    $subject="E-mail with attachment"; 

    // 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"; 

    // store the file information to variables for easier access 
    $tmp_name = $_FILES['filename']['tmp_name']; 
    $type = $_FILES['filename']['type']; 

    $size = $_FILES['filename']['size']; 

    // here we'll hard code a text message 
    // again, in reality, you'll normally get this from the form submission 
    $message = 'Message from '.$_REQUEST['name'].' 


    Full Name: '.$_REQUEST['name'].' 
    Address: '.$_REQUEST['address'].' 
    Contact Number: '.$_REQUEST['contact'].' 
    Email: '.$_REQUEST['email'].' 
    How do you come to know about us: '.$_REQUEST['type'].' 
    Car Make: '.$_REQUEST['make'].' 
    Car Model: '.$_REQUEST['model'].' 
    Year: '.$_REQUEST['year'].' 
    Registration: '.$_REQUEST['registration'].' 
    Comment: '.$_REQUEST['comments'].' 
    '; 

    // 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 build the message headers 
     $headers = "From: $from\r\n" . 
     "MIME-Version: 1.0\r\n" . 
     "Content-Type: multipart/mixed;\r\n" . 
     " boundary=\"{$mime_boundary}\""; 

     // next, we'll build 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 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 and set another boundary to 
     // indicate that the end of the file has been reached 
     $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" . 
     "--{$mime_boundary}--\n"; 

     // now we just send the message 
     if (@mail($to, $subject, $message, $headers)){ 
     echo "<script>alert('Your details has been Sent Successfully, we shall revert back to you shortly!!!');</script>"; 
     echo "<script>window.location.href='index.php';</script>"; 
     } else { 
     echo "<script>alert('Sending Failed');</script>"; 
     echo "<script>window.location.href='index.php';</script>"; 
    } } 
} else { 
    echo "<script>window.location.href='index.php?cap=invalid';</script>"; 
} 
?> 

這裏的形式:

<div class="quote-wrap left"> 
<form name="quoteForm" enctype="multipart/form-data" method="post" action="mail.php" onsubmit="return validateQuoteForm();"> 
<div class="quote-table left liquid-size"> 
<table width="100%"> 
    <tr> 
     <td colspan="2" align="center" valign="middle" style="padding-bottom:0px;"><?php $qerror ?></td> 
    </tr>            
    <tr> 
     <td colspan="2" align="center" valign="middle" style="padding-bottom:0px; color:#FF0000;"><h3><?php $captcha_stat?></h3></td> 
    </tr> 
    <tr> 
     <td valign="middle" align="left"> 
     <input type="text" name="make" placeholder="Make:" class="input-field" value="$make" /> 
     <span class="required">*</span> 
     </td> 
    </tr> 
     <tr> 
     <td valign="middle" align="left"> 
     <input type="text" name="model" placeholder="Model:" class="input-field" value="$model" /><span class="required">*</span></td> 
    </tr> 
    <tr> 
     <td valign="middle" align="left"> 
     <input type="text" name="year" placeholder="Year:" class="input-field" value="$year"/> 
     <span class="required">*</span>             
    </td> 
    </tr> 
     <tr> 
     <td valign="middle" align="left"> 
     <input type="text" name="registration" placeholder="Registration:" class="input-field" value="$registration" /><span class="required">*</span></td> 
    </tr> 
</table> 
</div> 

<div class="quote-table left liquid-size"> 
<table width="100%"> 
    <tr> 
     <td valign="middle" align="left"> 
     <input type="text" name="address" placeholder="Full Address:" class="input-field" value="$address" /><span class="required">*</span></td> 
    </tr> 
    <tr> 
     <td valign="middle" align="left"> 
     <input type="text" name="name" placeholder="Name:" class="input-field" value="$name"/><span class="required">*</span></td> 
    </tr> 
    <tr> 
     <td valign="middle" align="left"> 
     <input type="text" name="contact" placeholder="Contact No:" class="input-field" value="$contact"/><span class="required">*</span></td> 
    </tr> 
    <tr> 
     <td valign="middle" align="left"> 
     <input type="text" name="email" placeholder="Email:" class="input-field" value="$email" /><span class="required">*</span></td> 
    </tr>           
    <tr> 
     <td valign="middle" align="left">How do you come to know about Us?</td> 
    </tr> 
    <tr> 
     <td valign="middle" align="left"> 
     <select class="quotable-select" name="type"> 
     <option value="Google search">Google search</option> 
     <option value="Yahoo search">Yahoo search</option> 
     <option value="Bing search">Bing search</option> 
     <option value="Hotfrog">Hotfrog</option> 
     <option value="Local Newspaper">Local Newspaper</option> 
     <option value="Magazine">Magazine</option> 
     <option value="Returning Customer">Returning Customer</option> 
     <option value="Heard about us">Heard about us</option> 
     <option value="Saw us on the road">Saw us on the road</option>          
     <option value="Referral">Referral</option> 
     <option value="Facebook">Facebook</option> 
     <option value="Trademe">Trademe</option> 
     <option value="Yellow Online">Yellow Online</option> 
     </select> 
     </td> 
    </tr> 
</table> 
</div> 

<table width="100%"> 
    <tr> 
     <td colspan="2" align="left" class="required" valign="middle"> 
     <span class="required">Note: Image must not be greater than 1MB.</span> 
     </td> 
    </tr> 
    <tr> 
     <td valign="middle" align="left" wid> 
      Attach Photos:&nbsp;&nbsp; 
      <input type="file" id="fileupload" name="filename" border="0" /> 
     </td> 
    </tr> 
     <tr> 
      <td valign="middle" align="left"> 
      <textarea class="quote-txtarea" placeholder="Comments:" name="comments" rows="3" cols="22">$comments</textarea> 
     </td> 
    </tr>            
    <tr>              
     <td valign="top" > 
      <img src="captcha.php" name="captcha" id="captcha" style="padding: 0px; margin: 0 0 -4px;" /> 
      <input class="input" type="text" name="captcha" style="width:100px; height: 20px; padding: 0px;" /> 
      <input type="submit" name="submit_quote" class="submit" value="Submit" /> 
     </td> 
    </tr> 
</table> 
</form> 
</div> 
+0

當你說「這不是驗證」時,你究竟是什麼意思? –

+0

何處形? –

+0

驗證在哪裏? – Arfeen

回答

0

您對您的最終echo行有一個語法錯誤,記得以分號結束行。你不會提供太多的信息來說明你的意思,而不是驗證 - 所以我只是在這裏猜測 - 但你正在使用@mail@符號將抑制錯誤。

所以嘗試刪除@符號,看看你得到任何更具描述性的錯誤,而且加入這條線附近開始打開錯誤報告:

error_reporting(E_ALL); 

而且,你將不會看到的所有時間都是錯誤的,因爲你沒有涵蓋所有可能的情況。這是你的代碼的簡化:

<?php 
    if($_SESSION["captcha"] == $_POST["captcha"]) { 
     if (file_exists($tmp_name)){ 
      if (@mail($to, $subject, $message, $headers)){ 
       // Success 
      } else { 
       // Send failed 
      } 
     } // <-- Add an else clause here! 
    } else { 
     // Invalid 
    } 
?> 

所以你沒有做任何事情,如果file_exists返回false。添加一個返回消息的else子句:

+0

請看看這個。 http://carsremoval.co.nz/uploads/ –

+0

等我發佈了錯誤的代碼。堅持一分鐘 –

+0

我想請求幫助。正如你可以看到當你嘗試提交表單時,它並不是真正的工作,因爲我希望它能夠工作。如果用戶提交表單並輸入了錯誤的驗證碼,它應該說如果驗證成功則驗證無效,那麼應該說 echo「「; echo「」; 但現在上傳功能是一團糟。 –