Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.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 Can';单击html按钮后,不会回显任何内容_Javascript_Php_Html_Jquery_Contact Form - Fatal编程技术网

Javascript Can';单击html按钮后,不会回显任何内容

Javascript Can';单击html按钮后,不会回显任何内容,javascript,php,html,jquery,contact-form,Javascript,Php,Html,Jquery,Contact Form,我正在制作一个网站,需要一个联系人页面,当人们按send时可以给我发电子邮件。我有三个文件,一个是contactus.html,其中包括表单和发送按钮;一个是contact.js,它使用jquery验证contactus.html中的所有框是否已填写;另一个是contact\u process.php文件,它在单击发送按钮后向我发送电子邮件。到目前为止,代码基本上是有效的,每次都会向我发送一封电子邮件,如果有人试图在没有填写所有表格的情况下发送,他们就无法发送,但我想在发送邮件后回显一些信息,告

我正在制作一个网站,需要一个联系人页面,当人们按send时可以给我发电子邮件。我有三个文件,一个是contactus.html,其中包括表单和发送按钮;一个是contact.js,它使用jquery验证contactus.html中的所有框是否已填写;另一个是contact\u process.php文件,它在单击发送按钮后向我发送电子邮件。到目前为止,代码基本上是有效的,每次都会向我发送一封电子邮件,如果有人试图在没有填写所有表格的情况下发送,他们就无法发送,但我想在发送邮件后回显一些信息,告诉用户他们的邮件已收到

我使用了这段代码,它会发送电子邮件,但之后不会回显任何内容(您可能会注意到,从技术上讲,此表单允许某人键入任何内容,但仍然按send,但是,my contact.js会验证联系人表单是否为空)


我会像这样返回json:

if ($send) {
      $success = 1;
      $message = 'Some success message';
} else {
      $success = 0;
      $message = 'Some error message';
}
$data = [
   'success' => $success,
   'message' => $message ,
];
echo json_encode($data);
然后将我的成功处理程序更改为:

  success: function(response) {
    if (response.success) {
        $('#success').fadeIn()
        // show response.message in the html somewhere
    } else {
        $('#error').fadeIn()
        // show response.message in the html somewhere     
    }
    // ... other stuff...
  },
主要的一点是,当您从php脚本回显数据时,调用ajax的js需要检查响应并对其进行处理。我选择回显json而不仅仅是字符串消息,因为我认为它更干净,尽管您也可以这样做


有关此方法的更多信息,请参阅。

您没有关闭表单吗?我在任何地方都看不到
,甚至在进入编辑模式时也看不到。FWIW,您的表单不应该依赖客户端验证,您的后端还应该验证所需的输入是否正确present@FunkFortyNiner是的,我只是不小心把它从我粘贴的代码中漏掉了,我现在已经更新了。@WesleySmith My contact.js验证所有字段是否都已填写,我是否应该从我的PHPresearch ajax中删除开头的
if(isset($\u POST['message'])、isset($\u POST['name'])、isset($\u POST['email'])、isset($\u POST['number'])、isset($\u POST['subject'])
,虽然你也可以复制粘贴下面的答案
$(document).ready(function(){

(function($) {
    "use strict";


jQuery.validator.addMethod('answercheck', function (value, element) {
    return this.optional(element) || /^\bcat\b$/.test(value)
}, "type the correct answer -_-");

// validate contactForm form
$(function() {
    $('#contactForm').validate({
        rules: {
            name: {
                required: true,
            },
            subject: {
                required: true,
            },
            number: {
                required: true,
            },
            email: {
                required: true,
                email: true
            },
            message: {
                required: true,
            }
        },
        messages: {
            name: {
                required: "You must enter a name.",
            },
            subject: {
                required: "You must enter a subject.",
            },
            number: {
                required: "You must enter a phone number.",
            },
            email: {
                required: "You must enter a email."
            },
            message: {
                required: "You must enter a message.",
            }
        },
        submitHandler: function(form) {
            $(form).ajaxSubmit({
                type:"POST",
                data: $(form).serialize(),
                url:"contact_process.php",
                success: function() {
                    $('#contactForm :input').attr('disabled', 'disabled');
                    $('#contactForm').fadeTo( "slow", 1, function() {
                        $(this).find(':input').attr('disabled', 'disabled');
                        $(this).find('label').css('cursor','default');
                        $('#success').fadeIn()
                        $('.modal').modal('hide');
                        $('#success').modal('show');
                    })
                },
                error: function() {
                    $('#contactForm').fadeTo( "slow", 1, function() {
                        $('#error').fadeIn()
                        $('.modal').modal('hide');
                        $('#error').modal('show');
                    })
                }
            })
        }
    })
})     
})(jQuery)
})
if ($send) {
      $success = 1;
      $message = 'Some success message';
} else {
      $success = 0;
      $message = 'Some error message';
}
$data = [
   'success' => $success,
   'message' => $message ,
];
echo json_encode($data);
  success: function(response) {
    if (response.success) {
        $('#success').fadeIn()
        // show response.message in the html somewhere
    } else {
        $('#error').fadeIn()
        // show response.message in the html somewhere     
    }
    // ... other stuff...
  },