2014-09-15 4 views
0

누군가이 페이지를 새로 고침 할 때이 양식이 제출되는 이유를 찾도록 도와 줄 수 있습니까? 유효성 검사는 정상적으로 작동하며 세부 사항은 제출시 제출됩니다. 유일한 문제는이 양식이 연락처 페이지를 새로 고침 할 때 제출된다는 것입니다.PHP 코드가있는 양식이 페이지 새로 고침시 제출됩니다

<!doctype html> 
<html lang="en-US" xmlns="http://www.w3.org/1999/xhtml" class="csstransforms csstransforms3d csstransitions"><head profile="http://gmpg.org/xfn/11"> 
<meta charset="utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> 
<title>Our Site</title> 
<link href="media-queries.css" rel="stylesheet" type="text/css"> 
<link href="style.css" rel="stylesheet" type="text/css" /> 
<link href="fonts.css" rel="stylesheet" type="text/css"> 
</head> 
<body> 
<div id="pagewrap"> <!--pagewrap start--> 
<!--header start--> 
<div id="header"> 
    <div class="bottom"> 
    </div><!--bottom end--> 
</div> <!--header end--> 
<!--content start--> 
<div id="content_1"> 
    <div class="content_1_1"> 
    <span class="text9">Contact Us</span><br><hr style="width:670px; margin-left:90px"> 
    <div style="margin-left:90px; margin-right:0px"> 
     <?php 
     $firstname = $lastname = $email = $telephone = $comments = ""; 
     $error = ""; 

     if ($_SERVER["REQUEST_METHOD"] == "POST") { 

      if (empty($_POST["firstname"])) { 
       $error = "Field required"; 
      } else { 
       $firstname = check($_POST["firstname"]); 
       // check if name only contains letters and whitespace 
       if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) { 
        $error = "Field required"; 
       } 
      } 

      if (empty($_POST["lastname"])) { 
       $error = "Field required"; 
      } else { 
       $lastname = check($_POST["lastname"]); 
       // check if name only contains letters and whitespace 
       if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) { 
        $error = "Field required"; 
       } 
      } 


      if (empty($_POST["email"])) { 
       $error = "Field required"; 
      } else { 
       $email = check($_POST["email"]); 
       if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
        $error = "Field required"; 
       } 
      } 

      if (empty($_POST["telephone"])) { 
       $error = "Field required"; 
      } else { 
       $telephone = check($_POST["telephone"]); 
       if (!preg_match("/^[0-9\_]{7,20}/",$telephone)) { 
        $error = "Field required"; } 
      } 



      if (empty($_POST["comments"])) { 
       $errors = "Field required"; 
      } else { 
       $comments = check($_POST["comments"]); 
       if (!preg_match("/^[a-zA-Z ]*$/",$comments)) { 
        $error = "Field required"; 
       } 
      } 
     } 



     function check($data) { 
      $data = trim($data); 
      $data = stripslashes($data); 
      $data = htmlspecialchars($data); 
      return $data; 
     } 




     if (empty($error)) { 
      $from = "From: Our Site!"; //Site name 
      // Change this to your email address you want to form sent to 
      $to = "[email protected]"; 
      $subject = "Website Form " . $name . ""; 

      $message = "Message from " . $firstname . " " . $lastname . " 
      Email: " . $email ." 
      Phone: " . $telephone . " 
      Comments: " . $comments .""; 
      mail($to,$subject,$message,$from); 
     } 
     ?> 
     <form name="contactform" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" > 
     <table width="450px"> 
      <tr> 
      <td valign="top"> 
       <label for="firstname">First Name *</label> 
      </td> 
      <td valign="top"> 
       <input type="text" name="firstname" maxlength="50" size="30"> 
       <span class="error" style="color:#C00;"><?php echo $error ;?></span> 
      </td> 
      </tr> 
      <tr> 
      <td valign="top"> 
       <label for="lastname"> Last Name *</label> 
      </td> 
      <td valign="top"> 
       <input type="text" name="lastname" maxlength="50" size="30"> 
       <span class="error" style="color:#C00;"><?php echo $error ;?></span> 
      </td> 
      </tr> 
      <tr> 
      <td valign="top"> 
       <label for="email">Email Address *</label> 
      </td> 
      <td valign="top"> 
       <input type="email" name="email" maxlength="80" size="30"> 
       <span class="error" style="color:#C00;"><?php echo $error ;?></span> 
      </td> 
      </tr> 
      <tr> 
      <td valign="top"> 
       <label for="telephone">Telephone Number</label> 
      </td> 
      <td valign="top"> 
       <input type="number" name="telephone" maxlength="30" size="30"> 
       <span class="error" style="color:#C00;"><?php echo $error ;?></span> 
      </td> 
      </tr> 
      <tr> 
      <td valign="top"> 
       <label for="comments">Comments *</label> 
      </td> 
      <td valign="top"> 
       <textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea> 
       <span class="error" style="color:#C00;"><?php echo $error ;?></span> 
      </td> 
      </tr> 
      <tr> 
      <td colspan="2" style="text-align:center"> 
       <input type="submit" value="submit" name="submit"> 
      </td> 
      </tr> 
     </table> 
     </form> 
     <br> 
    </div> 
    </div> <!--content_1_1 end--> 
    </div><!--content end--> 
    <!--footer start--> 
    <div id="footer"> 
    </div><!--footer end--> 
    </div><!--pagewrap end--> 
    </body> 
</html> 
+0

가장 간단한 방법은 리디렉션을 추가하는 것 성공적으로 다시 리디렉션 같은 페이지로 그런 다음 사용자가 페이지를 새로 고치면 요청에 POST 데이터가 없습니다. – FlabbyRabbit

+0

하지만 처음으로 페이지를 열면 자동으로 우편으로 발송됩니다. – user3355627

답변

0

당신은 사용 : 새로 고침 $error

if (empty($error)) { 

mail(); 

} 

아마 비어, 따라서 양식을 우편으로 발송됩니다.

+0

예, 문제가되었지만 어떻게 해결할 수 있습니까? – user3355627

+0

'if ($ _SERVER [ "REQUEST_METHOD"] == "POST") {...} 블록 안에 모든 것을 넣으십시오. –

1

이미 이름이 지정된 제출 버튼이 있으므로 다음 조건문 안에 전체 PHP를 포함하십시오. 같은 조건문을 사용하는 동안

<?php 
if(isset($_POST['submit'])){ 
$firstname = $lastname = $email = $telephone = $comments = ""; 

... 

    mail($to,$subject,$message,$from); 
    } 

} // brace for if(isset($_POST['submit'])) 

?> 

그러나, 당신은 두 개의 별도 파일로 HTML 양식과 PHP를 나눌 수 있으며, 이후 다른 페이지로 리디렉션합니다.

리디렉션하는 방법에 대한 자세한 내용은, 스택에서 다음 Q & A 참조 : 양식을 제출하면

+0

양식이 제출되지 않습니다. 이 erorr 가져 오기 : 치명적인 오류 : /home/curesvot/public_html/curesight-contact.php의 정의되지 않은 함수 check() 호출 – user3355627

+0

@ user3355627 그런 다음'if ($ _SERVER [ "REQUEST_METHOD"] ==) 바로 위에 조건문을 설정해보십시오. "POST")' –

+0

@ user3355627 저에게있어서 전체 코드를 고치려고했으나 제대로 작동하지 않았습니다. 이 답변을 한 번 보시고 http://stackoverflow.com/a/10219610/을 참조하십시오. 그것이 Q & A 중 하나입니다. 사용중인 함수와 관련하여 스택에서 발견되었습니다. 코드를 디버깅 할 수 없습니다. 제출 단추의 이름과 조건문을 사용하여 내 대답을 그와 함께 사용하십시오. http://www.w3schools.com/php/php_form_required.asp에서 원본 코드를 얻었는지 확실하지는 않지만 닮았습니다. –

관련 문제