2014-01-11 148 views
0

我已經有了一個簡單的聯繫表單的基本的PHP腳本與名稱,電子郵件和消息輸入。
我希望在其中包含更多的選項,但不知道如何去做。我已經搜索,但找不到所有的解決方案。我想:PHP聯繫表格修改

1.發送一份拷貝給發件人的電子郵件
我想包括髮件人輸入有一個選項,收到一份的他提交給他的電子郵件,如果他checkes在該輸入形成。

2.上傳文件
此外,如果可能在相同的PHP腳本我希望給發件人到附加文件的可能性submiting形式時(只有優選IMG擴展)。

謝謝你的消息
不知道這一點,但現在我有一個簡單的感謝消息回聲時形式submited。如果可能的話,我希望此消息保持5秒鐘可見,然後重定向到index.html。

這裏是表單PHP:

<?php 

$name = $_POST['name']; 
$email = $_POST['email']; 
$message = $_POST['message']; 

$formcontent="Name: $name \nEmail: $email \nMessage: $message"; 
$recipient = "[email protected]"; 

$subject = "Contact"; 
$mailheader = "From: $email \r\n"; 
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!"); 

echo 
    "<div style='display: block; text-align: center;'>" . 
     "<span style='font-size: 14px;'>You message has been sent!</span>" . 
     "<a href='index.html'>Go back</a>" . 
    "</div>"; 
?> 

和演示的形式設置的jsfiddle

感謝您的任何幫助。

+3

這實在是3個問題,所有這些都得到解答:檢查是否變量後已發送(http://stackoverflow.com/questions/3496971/check-if-post-exists)用於向發件人發送可選副本,[上傳和附加文件到電子郵件](http://stackoverflow.com/questions/826265/simple-php-form-attachment-to-email-code-golf),最後, [5秒後重定向頁面](http://stackoverflow.com/questions/6119451/page-redirect-after-certain-time-php)。 –

+0

感謝Brodie的鏈接,我已經檢查過其中的2個,但我不熟悉PHP並結合這些方法,這就是爲什麼我要求可能的3/1解決方案。 – g5wx

回答

3

這是一個全球性的設置只是爲了讓你知道我會怎麼做這(如果我想這樣做1頁上的,但它是更好地使功能等)

編輯:請還要注意我不知道這是否有效。也許有錯誤,但我已經做了這個只是爲了讓你開始。

<?php 
      //Check if form submitted 
      if (isset($_POST)) { 

      //this all will run when form is submitted 


      //First sanitize you data thats been posted 
      $name = htmlentities($_POST['name'], ENT_QUOTES, 'UTF-8'); 
      $email = htmlentities($_POST['email'], ENT_QUOTES, 'UTF-8'); 
      $message = htmlentities($_POST['message'], ENT_QUOTES, 'UTF-8'); 

      //make a error array to hold errors 
      $error = array(); 
      //check if fields are not empty you can also do other checks 
      if (!empty($name) || !empty($email) || !empty($message)) 

      //here you could do extra checks.. like check if emai is really a email... 
       if(!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
        //email invalid 
      array_push($error, 'email not valid'); 
       } 
    //for image you could also do a if... 
      if(isset($_FILES)) { 
    $uploads_dir = 'YOUR DIR' 
       $name = $_FILES['image']['name']; 
       $type = $_FILES['image']['type']; 
       $size = $_FILES['image']['size']; 
       $temp = $_FILES['image']['tmp_name']; 
       $error = $_FILES['image']['error']; 

       if ($error === 4) { 
        //No file was selected 
        return false; 
       } 
       else 
       { 
    //do your stuff with the image here... 
    move_uploaded_file($temp, "$uploads_dir/$temp"); 
    } 

     ///you could do more ifs.. but if all is good then do the mail 

     $subject = 'new contact form message'; 
     $headers = 'From: [email protected]' . "\r\n" . 
      'Reply-To: [email protected]' . "\r\n" . 
      'X-Mailer: PHP/' . phpversion(); 

     mail($email, $subject, $message, $headers); 
     $success = "here the success message"; 
      } else { 
      //some fields are empty 
      array_push($error, 'some fields are empty'); 
      } 
      ?> 
      <!-- THE ENCTYPE IS NEEDED FOR IMAGES --> 


      <form action="submit.php" name="contact-form" id="contact-form" method="post" enctype="multipart/form-data"> 
      <input name="name" placeholder="Name" type="text" id="name" required /> 
      <input name="email" placeholder="Email" type="email" id="email" required /> 
      <textarea name="message" placeholder="Message" id="message" required></textarea> 
      <input type="file" id="upload-file" accept="image/*" /> 
      <div class="clear"></div> 
      <input type="checkbox" id="copy" name="copy" /> 
      <label for="copy">Send a copy to my email</label> 
      <div class="clear"></div> 
      <input type="submit" value="Submit" form="contact-form" name="submit-form" /> 
      </form> 

<?php 
if (isset($success) && !empty($success)) { 
//echo the success 
echo $success 
} 
if (isset($error) && !empty($error)) { 
//loop trough error 
foreach ($error as $e) { 
echo $e; 
} 
} 
?> 
+0

嘿謝謝,我會試試看,並希望它運作良好:) – g5wx

+0

好吧..它有錯誤..因爲我不看它靠近,但只是用這個作爲指導,當錯誤彈出讓我知道,我們可以看到最新的問題..如果這使你以正確的方式請接受awnser – rZaaaa

+0

嗨rZaaaa,對於遲到的答覆抱歉 - 標記爲接受。謝謝! – g5wx