2014-01-11 3 views
0

이름, 이메일 및 메시지 입력이있는 간단한 문의 양식을위한 기본 PHP 스크립트가 있습니다.
더 많은 옵션을 포함시키고 싶지만 어떻게해야할지 모르겠다. 검색했지만 찾지 못했습니다. 그는에 그 입력을 checkes 경우
나는 그가 제출 년대의 사본을 수신 할 수있는 옵션이 이메일의 송신자에 대한 입력을 포함시킬PHP 문의 양식 수정

1. 보낸 사람의 이메일 사본을 보내 : 나는 싶습니다 형태.

2. 업로드 내가 보낸 사람이 파일을 첨부 할 수있는 가능성 양식을 제출 중 (전용 바람직 IMG 확장)를 제공하고자하는 동일한 PHP 스크립트의 파일 또한
가능한 경우.

3.

당신에게이 약
확실하지 메시지
감사하지만, 지금은 양식 submited 때 간단한 에코 당신에게 메시지를 감사합니다. 가능하다면이 메시지가 5 초 동안 표시되고 index.html로 리디렉션되기를 바랍니다.

<?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 다음은

양식에 대한 PHP입니다.

도움 주셔서 감사합니다.

+3

이 정말 대답 한 모두 3 개 질문입니다 : [A POST 변수가 전송 된 경우 확인] (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

이 글로벌 설정 단지 당신은 내가 이런 짓을 했을까 방법을 알 수 있도록 (I 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

잘 .. 그것은 오류가 .. 왜냐하면 내가 그것을 가까이에서 볼 수 없기 때문에,하지만 그냥 가이드 라인으로 사용하고 오류가 날 팝업하자 그리고 우리는 문제가 뭔지 알 수 있습니다. 그리고 이것이 올바른 방법으로 이끌어 낸다면 pls는 awnser를 받아들입니다. – rZaaaa

+0

안녕하세요 rZaaaa, 늦은 답신에 대해 사과드립니다. 감사! – g5wx