jQuery - 防止过多的递归

时间:2015-03-24 22:48:04

标签: javascript jquery ajax

我尝试提交表单,但在控制台中收到错误“太多递归”。

如果有人可以提供帮助,这是我的代码

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 个答案:

答案 0 :(得分: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);
            }

        }