Javascript Jquery验证插件不发送POST数据

Javascript Jquery验证插件不发送POST数据,javascript,php,ajax,forms,jquery-validate,Javascript,Php,Ajax,Forms,Jquery Validate,更新2:我发现了问题所在!.htaccess文件中存在301重定向。一旦我被允许10岁以下的用户等待8小时,我会将其作为答案发布 更新:我采纳了Barmar的建议,检查了网络选项卡,这是一个我不太熟悉的选项卡,并注意到我收到了handle.php的301。请参见屏幕截图。我要做一些搜索和张贴我的结果 原始帖子:我正在使用验证并通过ajax发送表单数据。问题不是发送数据,而是表单处理程序说$\u POST数组中没有元素。我已经测试了几种不同的方法来发送ajax和数据,但是表单处理程序没有看到任何$

更新2:我发现了问题所在!.htaccess文件中存在301重定向。一旦我被允许10岁以下的用户等待8小时,我会将其作为答案发布

更新:我采纳了Barmar的建议,检查了网络选项卡,这是一个我不太熟悉的选项卡,并注意到我收到了handle.php的301。请参见屏幕截图。我要做一些搜索和张贴我的结果

原始帖子:我正在使用验证并通过ajax发送表单数据。问题不是发送数据,而是表单处理程序说$\u POST数组中没有元素。我已经测试了几种不同的方法来发送ajax和数据,但是表单处理程序没有看到任何$\u POST[]值

注意:我必须使用JQuery验证插件,因此它必须由.validate.submitHandler处理。任何$form.on都不够

html+jsindex.php 如您所见,serialize可以工作并获取所有信息,但是当由handle.php处理时,它告诉我$\u POST[]是空的。在handle.php上循环它就证明了这一点:

 foreach($_POST as $key=>$value) { 
   echo "$key: $value 
   \n"; 
}
一点也不回来。 我也尝试过ajaxSubmit和form.submit,但得到的结果完全相同

这一个看起来很对,因为我搜索了又搜索了stackoverflow,发现大多数问题都是在输入标记上包含了“name”属性,这已经完成了

提前谢谢

Page.html或.php

<form action="/" id="sky-form-modal" method=
"post" name="sky-form-modal">
  <input name="name" placeholder="Name" type="text">
  <input name="company" placeholder="Company" type="text">  
  <button class="button" type="submit">Send request</button>
</form>

<div id="result"></div>

<script>
var request;

$("#sky-form-modal").submit(function(event){
    // abort any pending request
    if (request) {
        request.abort();
    }
    var $form = $(this);
    var $inputs = $form.find("input, input");
    // serialize the data in the form
    var serializedData = $form.serialize();

    // let's disable the inputs for the duration of the ajax request
    // Note: we disable elements AFTER the form data has been serialized.
    // Disabled form elements will not be serialized.
    $inputs.prop("disabled", true);

    // fire off the request to /form.php
    request = $.ajax({
        url: "handle.php",
        type: "post",
        data: serializedData
    });

    // callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        // log a message to the console
        console.log("Hooray, it worked!");
        $("#result").html(response);
    });

    // callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // log the error to the console
        console.error(
            "The following error occured: "+
            textStatus, errorThrown
        );
    });

    // callback handler that will be called regardless
    // if the request failed or succeeded
    request.always(function () {
        // reenable the inputs
        $inputs.prop("disabled", false);
    });

    // prevent default posting of form
    event.preventDefault();
});
</script>
handle.php

<?php

foreach ($_POST as $key => $value) {
        echo "POST Key: '$key', Value: '$value'<br>";
    }

?>

为了表单的简单外观,我删除了您的标签和类。

我猜您在验证后错过了 $sky-form-modal.validate{
$sky-form-modal.validate{

我的问题与我的代码无关,最后是.htaccess中的一些声明。它将我从.php文件重定向到一个目录,以获得更漂亮的URL。现在,这是一种常见的技术,因此:


如果您正在处理其他人的项目,并且您的URL的文件扩展名不是标准的,请检查.htaccess!

您在开发人员工具的网络选项卡中看到了什么?handle.php是什么?Javascript代码发送到quote-request.php。无论出了什么问题,它都与jQuery验证插件无关。submitHandler启动点击一个有效的表单。你的jQuery.ajax方法负责发送数据。@Barmar-这是一个打字错误,已修复。我检查了我的“网络”选项卡,发现我在handle.php上的POST请求得到了一个301。查看上面的屏幕截图。是的,Sparky,你确实是对的,这是一个301问题,我现在正在研究301表示脚本正在发送重定向。这通常不适用于AJAX。我忘了提到我需要使用验证插件-
 foreach($_POST as $key=>$value) { 
   echo "$key: $value 
   \n"; 
}
<form action="/" id="sky-form-modal" method=
"post" name="sky-form-modal">
  <input name="name" placeholder="Name" type="text">
  <input name="company" placeholder="Company" type="text">  
  <button class="button" type="submit">Send request</button>
</form>

<div id="result"></div>

<script>
var request;

$("#sky-form-modal").submit(function(event){
    // abort any pending request
    if (request) {
        request.abort();
    }
    var $form = $(this);
    var $inputs = $form.find("input, input");
    // serialize the data in the form
    var serializedData = $form.serialize();

    // let's disable the inputs for the duration of the ajax request
    // Note: we disable elements AFTER the form data has been serialized.
    // Disabled form elements will not be serialized.
    $inputs.prop("disabled", true);

    // fire off the request to /form.php
    request = $.ajax({
        url: "handle.php",
        type: "post",
        data: serializedData
    });

    // callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        // log a message to the console
        console.log("Hooray, it worked!");
        $("#result").html(response);
    });

    // callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // log the error to the console
        console.error(
            "The following error occured: "+
            textStatus, errorThrown
        );
    });

    // callback handler that will be called regardless
    // if the request failed or succeeded
    request.always(function () {
        // reenable the inputs
        $inputs.prop("disabled", false);
    });

    // prevent default posting of form
    event.preventDefault();
});
</script>
<?php

foreach ($_POST as $key => $value) {
        echo "POST Key: '$key', Value: '$value'<br>";
    }

?>