2015-03-24 142 views
-2

我試圖提交一個表單,但在控制檯中出現錯誤「太多遞歸」。jQuery - 防止太多遞歸

這裏是我的代碼,如果有人能幫助

AJAX:

$(function() { 
    $('#form-contact').on('submit',function(event) { 
     event.preventDefault(); 

     $.ajax('formContact.php', { 
      data: $(this).serialize(), 
      success: function(result) { 

       $('#form-contact').each(function(){ 
        this.reset(); 
       }); 

      } 
     }); 
     return false; 
    }); 
}); 
+1

什麼是$('#formcontact')',爲什麼會有多個? – adeneo 2015-03-24 22:50:10

+0

adeneo ups我的壞我編輯:) – Jay 2015-03-24 22:52:13

+0

看起來真的不是什麼錯誤,據我所知'重置'不會觸發提交,所以不應該從該代碼的任何遞歸? – adeneo 2015-03-24 22:56:56

回答

0

adeneo我不這麼認爲,我將在這裏通過代碼

形式:

  <form role="form" action="<?=$base_url?>/contactus.php" method="POST" id="form-contact"> 
       <input type="text" name="nome" id="nome" placeholder="Nome"> 
       <input type="text" name="email" id="email" placeholder="Email"> 
       <input type="text" name="assunto" id="assunto" placeholder="Assunto"> 
       <textarea type="text" name="mensagem" id="mensagem" placeholder="Mensagem" maxlength="755"></textarea> 
      <input type="submit" name="submit" id="btn-cont" value="Enviar"> 
      <input type="hidden" name="subFormContact" value="true"> 
     </form> 

formcontact.php

 if($_POST["subFormContact"] == true){ 

      $nome = mysqli_real_escape_string($con,$_POST["nome"]); 
      $email = mysqli_real_escape_string($con,$_POST["email"]); 
      $assunto = mysqli_real_escape_string($con,$_POST["assunto"]); 
      $mensagem = mysqli_real_escape_string($con,$_POST["mensagem"]); 
      $data = date("Y-m-d H:i:s"); 

      $query_contactus = mysqli_query($con, "SELECT email FROM clientes_contact WHERE email = '{$email}'"); 
      if(($results = mysqli_num_rows($query_contactus) > 0)){ 


      }else{ 

       if(($_POST["nome"] == "") || ($_POST["assunto"] == "") || ($_POST["email"] == "") || ($_POST["mensagem"] == "")){ 


       }else{ 

        $sql = "INSERT INTO clientes_contact (nome,email,assunto,mensagem,data) 
          VALUES ('{$nome}','{$email}','{$assunto}','{$mensagem}','{$data}')"; 


        if(mysqli_query($con,$sql)){ 

         $mail = new PHPMailer; 

         $mail->CharSet = "UTF-8"; 
         $mail->isSMTP(); 
         $mail->Host = 'localhost'; 
         $mail->SMTPAuth = false; 
         $mail->Username = ''; 
         $mail->Password = ''; 
         $mail->SMTPSecure = 'tls'; 

         $mail->From = $_SESSION["email"]; 
         $mail->FromName = $_SESSION["empresa"]; 
         $mail->addAddress($_SESSION["email"]); 

         $mail->WordWrap = 50; 
         $mail->isHTML(true); 

         $mail->Subject = 'Admin - Mensagem cliente'; 
         $mail->Body = '<img src="'.$base_template_url.'/logo.png" alt="'.$_SESSION["empresa"].'" height="200" width="200"> 
              <h2>Ervanária Longa Vida</h2>'; 


         if(!$mail->send()){ 
          echo 'Message could not be sent.'; 
          echo 'Mailer Error: ' . $mail->ErrorInfo; 
          exit; 
         } 



        }else{ 


        } 
       } 

       mysqli_close($con); 
      } 

     }