Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试使用jQuery';时未提交表单;s顺从者_Javascript_Php_Jquery_Ajax_Forms - Fatal编程技术网

Javascript 尝试使用jQuery';时未提交表单;s顺从者

Javascript 尝试使用jQuery';时未提交表单;s顺从者,javascript,php,jquery,ajax,forms,Javascript,Php,Jquery,Ajax,Forms,我正在将联系人表单转换为AJAX,这样我就可以完成success功能。现在代码停在submitHandler部分,表示它未定义。我的提交人是如何定义的 submitHandler(函数(形式){ 但是,我这样做是否正确?我想使用当前的php,因为我将其构建为jQuery验证之上的后端验证 有人看到submitHandler有什么问题吗?还有什么可能阻止它发送的东西吗 HTML PHP $hasrerror=false; $sent=false; $name=$_POST['name']; $em

我正在将联系人表单转换为AJAX,这样我就可以完成success功能。现在代码停在submitHandler部分,表示它未定义。我的提交人是如何定义的

submitHandler(函数(形式){

但是,我这样做是否正确?我想使用当前的php,因为我将其构建为jQuery验证之上的后端验证

有人看到submitHandler有什么问题吗?还有什么可能阻止它发送的东西吗

HTML

PHP

$hasrerror=false;
$sent=false;
$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
如果(isset($_POST['submitform'])){
$name=trim(htmlspecialchars($_POST['name'],ENT_引号));
$email=trim(htmlspecialchars($_POST['email']);
$message=trim(htmlspecialchars($_POST['message'],ENT_引号));
$fieldsArray=数组(
“name”=>$name,
“email”=>$email,
'message'=>$message
);
$errorArray=array();
foreach($fieldsArray作为$key=>$val){
交换机($key){
案例“名称”:
案例“信息”:
如果(空($val)){
$hasError=true;
$ERRORRARY[$key]=ucfirst($key)。“字段留空。”;
}
打破
“电子邮件”案例:
如果(!filter_var($email,filter_VALIDATE_email)){
$hasError=true;
$errorArray[$key]=“输入的电子邮件地址无效”;
}否则{
$email=filter\u var($email,filter\u SANITIZE\u email);
}
打破
}
}
如果($hasError!==true){
$to=“gsdfa”;
$subject=“已提交联系方式”;
$msgcontents=“Name:$Name
电子邮件:$Email
消息:$Message”; $headers=“MIME版本:1.0\r\n”; $headers.=“内容类型:text/html;字符集=iso-8859-1\r\n”; $headers.=“发件人:$name\r\n”; $emailsent=mail($to、$subject、$msgcontents、$headers); 如果($mailssent){ $sent=true; unset($名称); 未结算(电子邮件); 未设置($消息); } } }
使用以下方法:

JQuery:

$(document).ready(function(){
   $("#submit_contact").click(function(){
         event.preventDefault();
        var name    = $("#name").val();
            var email   = $("#email").val();
            var message = $("#message").val();


                $.ajax({
                    url: "email-contact.php", 
                    type: "POST",
                    data: {
                        "project_name": name,
                        "title_roll":   email,
                        "project_email": message
                    },
                    success: function (data) {
                        //console.log(data); // data object will return the response when status code is 200
                        if (data == "Error!") {
                            alert("Unable to send email!");
                            alert(data);
                        } else {
                            $(".contact-container").addClass("removeClass");
                            $(".contact-email-success").show();
                            $(".announcement_success").fadeIn();
                            $(".announcement_success").show();
                           // $('.announcement_success').html('Email Successfully sent!');
                           //$('.announcement_success').delay(5000).fadeOut(400);
                        }
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        alert(textStatus + "|" + errorThrown);
                        //console.log("error"); //otherwise error if status code is other than 200.
                    }
                });
   });
});
在php文件更改中:

$name       = $_POST['name'];
$email      = $_POST['email'];
$message    = $_POST['message'];
进入


希望它有帮助

您需要在
validate()
函数调用内部定义
submitHandler
。但您只是想在外部执行它。请这样做-

$("#contactform").validate({
        rules: {...},
        messages: {...},
        submitHandler: function(form) {
            //Variables for the form ids
            var name    = $("#name").val();
            var email   = $("#email").val();
            var message = $("#message").val();


            $.ajax({
                url: "email-contact.php", 
                type: "POST",
                data: {
                    "project_name": name,
                    "title_roll":   email,
                    "project_email": message
                },
                success: function (data) {
                    //console.log(data); // data object will return the response when status code is 200
                    if (data == "Error!") {
                        alert("Unable to send email!");
                        alert(data);
                    } else {
                        $(".contact-container").addClass("removeClass");
                        $(".contact-email-success").show();
                        $(".announcement_success").fadeIn();
                        $(".announcement_success").show();
                       // $('.announcement_success').html('Email Successfully sent!');
                       //$('.announcement_success').delay(5000).fadeOut(400);
                    }
                },
                error: function (xhr, textStatus, errorThrown) {
                    alert(textStatus + "|" + errorThrown);
                    //console.log("error"); //otherwise error if status code is other than 200.
                }
            });
        }
});

请参阅文档中的示例。

是否有错误reporting@devpro是的,它是说我的
submitHandler(函数(形式){
没有定义。你添加了jquery库吗?使用event.preventDefault();在submitHandler中…是的,我有jquery库。当我放入偶数.preventDefault()时在submithandler内部,它会重新加载页面,但在我点击submit之前,它仍然说submithandler没有定义。yea missing“),tnxI修改了这个,并保存在我的validate中…现在不起作用…但它正在发送到我的php文件,但它没有做任何事情。请打开你的inspect元素(f12)然后转到“网络”选项卡,提出新请求并说明xhr请求中发生了什么我也没有从email-contact.php中得到网络选项卡的响应。在标题中,它显示的是表单数据:contact\u name:gfdsa contact\u email:bopb contact\u message:gfdsagaSo,我将在他是submitHandler?是的,只需使用submitHandler函数中的相同代码。这并不是说submitHandler是意外的标识,您是说添加这个吗?`$(form).ajaxSubmit()?
$(document).ready(function(){
   $("#submit_contact").click(function(){
         event.preventDefault();
        var name    = $("#name").val();
            var email   = $("#email").val();
            var message = $("#message").val();


                $.ajax({
                    url: "email-contact.php", 
                    type: "POST",
                    data: {
                        "project_name": name,
                        "title_roll":   email,
                        "project_email": message
                    },
                    success: function (data) {
                        //console.log(data); // data object will return the response when status code is 200
                        if (data == "Error!") {
                            alert("Unable to send email!");
                            alert(data);
                        } else {
                            $(".contact-container").addClass("removeClass");
                            $(".contact-email-success").show();
                            $(".announcement_success").fadeIn();
                            $(".announcement_success").show();
                           // $('.announcement_success').html('Email Successfully sent!');
                           //$('.announcement_success').delay(5000).fadeOut(400);
                        }
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        alert(textStatus + "|" + errorThrown);
                        //console.log("error"); //otherwise error if status code is other than 200.
                    }
                });
   });
});
$name       = $_POST['name'];
$email      = $_POST['email'];
$message    = $_POST['message'];
$name       = $_POST['project_name'];
$email      = $_POST['title_roll'];
$message    = $_POST['project_email'];
$("#contactform").validate({
        rules: {...},
        messages: {...},
        submitHandler: function(form) {
            //Variables for the form ids
            var name    = $("#name").val();
            var email   = $("#email").val();
            var message = $("#message").val();


            $.ajax({
                url: "email-contact.php", 
                type: "POST",
                data: {
                    "project_name": name,
                    "title_roll":   email,
                    "project_email": message
                },
                success: function (data) {
                    //console.log(data); // data object will return the response when status code is 200
                    if (data == "Error!") {
                        alert("Unable to send email!");
                        alert(data);
                    } else {
                        $(".contact-container").addClass("removeClass");
                        $(".contact-email-success").show();
                        $(".announcement_success").fadeIn();
                        $(".announcement_success").show();
                       // $('.announcement_success').html('Email Successfully sent!');
                       //$('.announcement_success').delay(5000).fadeOut(400);
                    }
                },
                error: function (xhr, textStatus, errorThrown) {
                    alert(textStatus + "|" + errorThrown);
                    //console.log("error"); //otherwise error if status code is other than 200.
                }
            });
        }
});