2011-11-29 48 views
0

我有一個表單不會將圖像發送到我的電子郵件。在表單上,​​用戶可以上傳兩張圖片,並將所有信息提交給我的電子郵件,但圖片沒有。表單不發送圖像到電子郵件

這是形式的代碼:

<div id="stylized" class="myform"> 
<form id="myform" name="myform" action="bookingform.php" method="post" > 
<h1></h1> 
<p></p> 

<div id="firstname"> 
<label>Name 
<span class="small">Add your name</span> 
</label> 
<input type="text" name="FirstName" id="FirstName" /> 
</div> 

<div id="email"> 
<label>Email 
<span class="small">Add a valid email address</span> 
</label> 
<input type="text" name="Email" id="Email" /> 
</div> 

<div id="phone"> 
<label>Tel Number 
<span class="small">Add phone number</span> 
</label> 
<input type="text" name="Phone" id="Phone" /> 
</div> 


<label>Address 
<span class="small">Add your address</span> 
</label> 
<input type="text" name="Address" id="Address" /> 


<h2 style="margin-top:140px;">Event Venue Information</h2> 

<div id="dates"> 
<label>Date of Event 
<span class="small">Date of your occasion</span> 
</label> 
<input type="text" name="datex" id="datex" /> 
</div> 
<div> 
<label>Occasion 
<span class="small">Type of occasion</span> 
</label> 
<input type="text" name="type" id="type" /> 
</div> 

<div style="margin-left:315px;"> 
<label>Venue 
<span class="small">Location of event</span> 
</label> 
<input type="text" name="location" id="location" /> 
</div> 
<div style="float:right; margin-right:25px;"> 
<label>Services Required 
<span class="small">Service needed</span> 
</label> 
<textarea cols="40" rows="5" name="services"> 
</textarea> 
</div> 


<div style="margin-top:-20px;"> 
<label>Please photo's that you find are useful:</label> 
<input type="file" name="image" size="40" 
onchange="return validateFileExtension(this)"> 
<input type="file" name="image" size="40" 
onchange="return validateFileExtension(this)"> 
</div> 


<h2 style="margin-top:260px;">Additional Information</h2> 

<div style="margin-left:-40px; margin-top:30px;"> 
<label>How you found us? 
<span class="small">Website/DJ's/Friend</span> 
</label> 
<input type="text" name="found" id="found" /> 
</div> 

<div style="float:right; margin-right:10px;"> 
<label>Your comments/questions 
<span class="small">Any other queries</span> 
</label> 
<textarea cols="40" rows="5" name="questions"> 
</textarea> 
</div> 
<div> 
<input class="buttons" style="margin-top:10px; margin-left:110px;" type="submit" 
value="Submit" 
></input> 
</div> 

<div class="spacer"></div> 

</form> 
<script language="JavaScript" type="text/javascript" 
    xml:space="preserve">//<![CDATA[ 
//You should create the validator only after the definition of the HTML form 
    var frmvalidator = new Validator("myform"); 
    frmvalidator.addValidation("FirstName","req","Please enter your First Name"); 
    frmvalidator.addValidation("FirstName","maxlen=20", "Max length for FirstName is 20"); 
    frmvalidator.addValidation("FirstName","alpha","Alphabetic chars only"); 



    frmvalidator.addValidation("Email","maxlen=50"); 
    frmvalidator.addValidation("Email","req"); 
    frmvalidator.addValidation("Email","email"); 

    frmvalidator.addValidation("Phone","maxlen=50"); 
    frmvalidator.addValidation("Phone","numeric"); 

    frmvalidator.addValidation("Address","maxlen=50"); 


//]]></script> 
</div> 

這是PHP代碼:

<?php 
$boundary = uniqid('np'); 

$field_name = $_POST['FirstName']; 
$field_email = $_POST['Email']; 
$field_phone = $_POST['Phone']; 
$field_address = $_POST['Address']; 
$field_date = $_POST['datex']; 
$field_type = $_POST['type']; 
$field_location = $_POST['location']; 
$field_services = $_POST['services']; 
$field_found = $_POST['found']; 
$field_comments = $_POST['questions']; 

$mail_to = '[email protected]'; 


$body_message = 'From: '.$field_name."\n"; 
$body_message .= 'E-mail: '.$field_email."\n"; 
$body_message .= 'Phone: '.$field_phone."\n"; 
$body_message .= 'Address: '.$field_address."\n"; 
$body_message .= 'Date: '.$field_date."\n"; 
$body_message .= 'Type of Occasion: '.$field_type."\n"; 
$body_message .= 'Location: '.$field_location."\n"; 
$body_message .= 'Services required: '.$field_services."\n"; 
$body_message .= 'How they found us: '.$field_found."\n"; 
$body_message .= 'Comments - Questions: '.$field_comments; 

$headers = "MIME-Version: 1.0\r\n"; 
$headers = 'From: '.$Email."\r\n"; 

$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n"; 


$message = "This is a MIME encoded message."; 

$message .= "\r\n\r\n--" . $boundary . "\r\n"; 
$message .= "Content-type: text/plain;charset=utf-8\r\n\r\n"; 
$message .= "This is the text/plain version."; 

$message .= "\r\n\r\n--" . $boundary . "\r\n"; 
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n"; 
$message .= "This is the <b>text/html</b> version."; 

$message .= "\r\n\r\n--" . $boundary . "--"; 



$mail_status = mail($mail_to, $subject, $body_message, $headers); 

if ($mail_status) { ?> 
    <script language="javascript" type="text/javascript"> 
     alert('Thank you for the message. We will contact you shortly.'); 
     window.location = 'http://www.google.co.uk'; 
    </script> 
<?php 
} 
else { ?> 
    <script language="javascript" type="text/javascript"> 
     alert('Message failed. Please, send an email to [email protected]'); 
     window.location = 'url'; 
    </script> 
<?php 
} 
?> 

我會很感激,如果有人可以告訴我如何使圖像與發表格。

感謝

編輯:

我已嘗試添加爲一個單獨的腳本,但它給出了一個口口聲聲說意外}這是代碼中的錯誤:

//reads the name of the file the user submitted for uploading 
    $image=$_FILES['image']['name']; 
    //if it is not empty 
    if ($image) 
    { 
    //get the original name of the file from the clients machine 
     $filename = stripslashes($_FILES['image']['name']); 
    //get the extension of the file in a lower case format 
     $extension = getExtension($filename); 
     $extension = strtolower($extension); 
    //if it is not a known extension, we will suppose it is an error and will not upload the file, 
    //otherwise we will do more tests 
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
     { 
     //print error message 
      echo '<h1>Unknown extension!</h1>'; 
      $errors=1; 
     } 
     else 
     { 
//get the size of the image in bytes 
//$_FILES['image']['tmp_name'] is the temporary filename of the file 
//in which the uploaded file was stored on the server 
$size=filesize($_FILES['image']['tmp_name']); 

//compare the size with the maxim size we defined and print error if bigger 
if ($size > MAX_SIZE*1024) 
{ 
    echo '<h1>You have exceeded the size limit!</h1>'; 
    $errors=1; 
} 

//we will give an unique name, for example the time in unix time format 
$image_name=time().'.'.$extension; 
//the new name will be containing the full path where will be stored (images folder) 
$newname="images/".$image_name; 
//we verify if the image has been uploaded, and print error instead 
$copied = copy($_FILES['image']['tmp_name'], $newname); 
if (!$copied) 
{ 
    echo '<h1>Copy unsuccessfull!</h1>'; 
    $errors=1; 
}}}} 

//If no errors registred, print the success message 
if(isset($_POST['Submit']) && !$errors) 
{ 
    echo "<h1>File Uploaded Successfully! Try again!</h1>"; 
} 

?> 
+0

我沒有看到任何你甚至想要包含這些圖像。你是否在單獨的腳本中處理文件上傳? – TheOx

+0

@TheOx對不起,我已經編輯了一個腳本,我正在使用作爲一個單獨的文件。 – user1049553

+0

在你的文件上傳腳本中,你有一個額外的「}」在4個大括號中。我建議你使用縮進來設置你的代碼的格式,以便更容易地找到這些類型的錯誤。解決這個問題,然後看看你的上傳腳本是否有效。 – TheOx

回答

0

你有沒有添加enctype爲表格:

 
<form enctype="multipart/form-data" id="myform" name="myform" action="bookingform.php" method="post" > 

這可能是一個問題,希望它hel ps