2010-08-24 51 views
1

Arrg。php和jQuery聯繫表格中的無盡循環錯誤

我無法發現此聯繫表單中的無限循環。表單提交後,「謝謝」div無限制地打印出來。我錯過了什麼?是錯誤的PHP或也是jQuery? (在工作文件中,JS通過<script type="text/javascript" src="contact.js"></script>包括)

感謝

<?php 
if(isset($_POST['submitted'])) { 
if(trim($_POST['checking']) !== '') { 
$captchaError = true; 
} else { 

if(trim($_POST['email']) === '') { 
$emailError = 'Please enter a valid email address'; 
$hasError = true; 
} else if (!eregi("^[A-Z0-9._%-][email protected][A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) { 
$emailError = 'That\'s not a valid email address'; 
$hasError = true; 
} else { 
$email = trim($_POST['email']); 
} 
} 

if(!isset($hasError)) { 

$emailTo = 'to email'; 
$subject = 'site email'; 
$body = "$email"; 
$headers = 'From: webmail'; 
mail($emailTo, $subject, $body, $headers); 
$emailSent = true; 
} 
} ?> 


<script type="text/javascript"> 

$(document).ready(function() { 
    $('form#contactForm').submit(function() { 
     $('form#contactForm .error').remove(); 
     var hasError = false; 
     $('.requiredField').each(function() { 
      if($(this).hasClass('email')) { 
       var emailReg = /^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/; 
       if(!emailReg.test(jQuery.trim($(this).val()))) { 
        var labelText = $(this).prev('label').text(); 
        $(this).parent().append('<span class="error">&nbsp;&nbsp;<i>invalid email</i></span>'); 

        hasError = true; 
       } 
      } 
     }); 
     if(!hasError) { 
      $('#thanks').fadeOut('normal', function() { 
$(this).parent().append('<img src="../loading-small.gif" alt="Loading&hellip;" height="31" width="31" />'); 

      }); 
      var formInput = $(this).serialize(); 
      $.post($(this).attr('action'),formInput, function(data){ 
       $('form#contactForm').slideUp("fast", function() {     
        $(this).before('<p class="thanks">&nbsp;<strong>Thanks!</strong>'); 
        $(".thanks").delay(3000).fadeOut(); 
       }); 
      }); 
     } 

     return false; 

    }); 
}); 

</script> 


<?php if(isset($emailSent) && $emailSent == true) { ?> 

<div class="thanks"></div> 

<?php } else { ?> 

<form action="http://mysite.com" id="contactForm" method="post"> 

Sign up for email notification <input type="text" name="email" size="30" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="requiredField email" /> 

<?php if($emailError != '') { ?><span class="error"><?=$emailError;?></span><?php } ?> 

<input type="hidden" name="submitted" id="submitted" value="true" /> 
<button type="submit">Send</form> 

<?php } ?> 
+0

你不信到代碼縮進在PHP? – Tomalak 2010-08-24 16:33:34

+0

這一切都在一個文件中? – Stian 2010-08-24 16:33:35

+0

還沒有學過代碼縮進:)想想我需要的。而JS通常包含在URL中(上面編輯的細節) – markratledge 2010-08-24 16:52:51

回答

1

我做了一些改變你的代碼,並得到它我的本地機器上工作,看看這一個適合你

<?php 
if(isset($_POST['submitted'])) { 
    if(trim($_POST['checking']) !== '') { 
     $captchaError = true; 
    }else{ 
     if(trim($_POST['email']) === '') { 
      $emailError = 'Please enter a valid email address'; 
      $hasError = true; 
     } else if (!eregi("^[A-Z0-9._%-][email protected][A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) { 
      $emailError = 'That\'s not a valid email address'; 
      $hasError = true; 
     } else { 
      $email = trim($_POST['email']); 
     } 
    } 

    if(!isset($hasError)) { 
     $emailTo = 'to email'; 
     $subject = 'site email'; 
     $body = "$email"; 
     $headers = 'From: webmail'; 
     mail($emailTo, $subject, $body, $headers); 
     $emailSent = true; 
    } 
} 
?> 
<html> 
    <head> 
     <title></title> 
     <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script> 
     <script type="text/javascript" src="js/jquery-ui.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $('form#contactForm').submit(function() { 
       $('.error').remove(); //remove the existing error messages before we submit 
       var hasError = false; 
       //lets go through all the required feilds 
       $('.requiredField').each(function() { 
        //check to see if we are processing email 
        if($(this).hasClass('email')) { 
         var emailReg = /^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/; 
         if(!emailReg.test(jQuery.trim($(this).val()))) { 
          // var labelText = $(this).prev('label').text(); //this line seem useless 
          $(this).parent().append('<span class="error">&nbsp;&nbsp;<i>invalid email</i></span>'); // supply eror message 
          hasError = true; 
         } 
        } 
       }); 
       if(!hasError) { 
        $('#thanks').fadeOut('normal', function() { 
         $(this).parent().append('<img src="../loading-small.gif" alt="Loading&hellip;" height="31" width="31" />'); 
        }); 
        var formInput = $(this).serialize(); 
        $.post($(this).attr('action'),formInput, function(data){ 
         $('form#contactForm').slideUp("fast", function() {     
          $(this).before('<p class="thanks">&nbsp;<strong>Thanks!</strong>'); 
          $(".thanks").delay(3000).fadeOut(); 
         }); 
        }); 
       } 
       return false; 
      }); 
     }); 

    </script> 
</head> 
<body> 



<?php if(isset($emailSent) && $emailSent == true) { ?> 

<div class="thanks"></div> 

<?php } else { ?> 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactForm" method="post"> 

Sign up for email notification <input type="text" name="email" size="30" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="requiredField email" /> 

<?php if($emailError != '') { ?><span class="error"><?=$emailError;?></span><?php } ?> 

<input type="hidden" name="submitted" id="submitted" value="true" /> 
<button type="submit">Send</form> 

<?php } ?> 
    </body> 
</html> 
+0

謝謝!現在起作用了。代碼註釋很好理解。將比較代碼,看看我做錯了什麼.... – markratledge 2010-08-24 18:00:51

+0

高興我可以幫助.....代碼評論主要是爲了我的理智 – mcgrailm 2010-08-24 18:19:12

1

它看起來像你想同時具有現有元素(DIV)同時添加元素(<p class="thanks")。你能縮小哪一個?你也有$("#thanks").fadeOut,但似乎沒有任何元素,idthanks

我建議仔細閱讀代碼。我沒有看到一個無限循環,但如果出於某種原因反覆觸發該功能,則會出現相同的症狀。