2011-03-04 8 views
1

나는 (단일 페이지 비즈니스/포트폴리오) 웹 사이트의 하단에 문의 양식을 가지고 있습니다.문의 양식 : 왜이 ​​양식의 유효성을 검사하지 않습니까?

내 DOCTYPE 위의 스크립트는 다음과 같습니다.

<?php 
//If the form is submitted 
if(isset($_POST['submit'])) { 

    //Check to make sure that the name field is not empty 
    if(trim($_POST['contactname']) == '') { 
     $hasError = true; 
    } else { 
     $name = trim($_POST['contactname']); 
    } 

    //Check to make sure that the subject field is not empty 
    if(trim($_POST['subject']) == '') { 
     $hasError = true; 
    } else { 
     $subject = trim($_POST['subject']); 
    } 

    //Check to make sure sure that a valid email address is submitted 
    if(trim($_POST['email']) == '') { 
     $hasError = true; 
    } else if (!eregi("^[A-Z0-9._%-][email protected][A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) { 
     $hasError = true; 
    } else { 
     $email = trim($_POST['email']); 
    } 

    //Check to make sure comments were entered 
    if(trim($_POST['comment']) == '') { 
     $hasError = true; 
    } else { 
     if(function_exists('stripslashes')) { 
      $comments = stripslashes(trim($_POST['message'])); 
     } else { 
      $comments = trim($_POST['message']); 
     } 
    } 

    //If there is no error, send the email 
    if(!isset($hasError)) { 
     $emailTo = '[email protected]'; //Put your own email address here 
     $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments"; 
     $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; 

     mail($emailTo, $subject, $body, $headers); 
     $emailSent = true; 
    } 
} 
?> 

JQuery 유효성 검사가 작동합니다. 양식을 제출하면 이메일이 전송되지 않습니다

<div class="grid_8"> 
    <?php if(isset($hasError)) { //If errors are found ?> 
     <p class="error">Please check if you've filled all the fields with valid information. Thank you.</p> 
    <?php } ?> 

    <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?> 
     <p><strong>Email Successfully Sent!</strong></p> 
     <p>Thank you <strong><?php echo $name;?></strong> for using my contact form! Your email was successfully sent and I will be in touch with you soon.</p> 
    <?php } ?> 
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
     <div> 
      <label for="name">Your Name:</label> 
      <div> 
       <input type="text" name="contactname" class="required" /> 
      </div> 
     </div> 
     <div> 
      <label for="email">Your Email:</label> 
      <div> 
       <input type="text" name="email" class="required email" /> 
      </div> 
     </div> 
     <div> 
      <label for="subject">Subject:</label> 
      <div> 
       <input type="text" name="subject" class="required" /> 
      </div> 
     </div> 
     <div> 
      <label for="comments">Comments:</label> 
      <div> 
       <textarea name="comment" name="comment" class="required"></textarea> 
      </div> 
     </div> 
     <div> 
      <input id="button" type="submit" value="SEND" /> 
     </div> 
    </form> 
    </div> 

않으며, PHP에서 어떤 검증 :

<script type="text/javascript"> 
$(document).ready(function(){ 
    $("form").validate(); 
}); 
</script> 

문의 양식. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+0

eregi()는 PHP 5.3.0부터 사용되지 않습니다 –

답변

2

한 빠른 명백한 문제에 대한 질문 사람. 간단히 너를 넣으려고 $_POST['submit']을 넣으면 양식에 name="submit"이있다.

그래서 변경 :

<input id="button" type="submit" value="SEND" /> 

사람 :

<input id="button" type="submit" name="submit" value="SEND" /> 

또는 변경 :

if(isset($_POST['submit'])) 

사람 :

if(count($_POST)) 
// OR 
if(!empty($_POST)) 

어느 하나가 문제를 해결할 것입니다

2

양식에 name="submit"이없는 필드가 없는데 왜 isset($_POST['submit'])이 true를 반환하는지 알 수 없습니다.

내가 필요한 모든 데이터를 배열을 사용 할 선호 : 나는 많은 덜 중요했지만, 다음

<!-- ... --> 
<input type="text" name="mail[contactname]" class="required" /> 
<!-- ... --> 
<input type="text" name="mail[email]" class="required email" /> 
<!-- ... --> 
<input type="text" name="mail[subject]" class="required" /> 
<!-- ... --> 
<textarea name="comment" name="mail[comment]" class="required"></textarea> 
<!-- ... --> 

및 해당 배열

if (isset($_POST['mail'])) 
    // ... 
관련 문제