Jquery/Ajax-formPost数据不起作用


Jquery/Ajax form Post data is not working

我不是Jquery的专家。我已经构建了一个类似的4步html表单

<form id="msform" enctype="multipart/form-data">
    <fieldset id="publish1" data-check-id="1">
        //some inputs
    </fieldset>
    <fieldset id="publish2" data-check-id="2">
        //some inputs
    </fieldset>
    <fieldset id="publish3" data-check-id="3">
        //some inputs
    </fieldset>
    <fieldset id="publish4" data-check-id="4">
        <input type="submit" class="submit action-button pull-right top-35" value="Publish"/>
    </fieldset>
</form>

在我的.js文件中写了一些Jquery验证之后,我尝试通过ajax将数据传递到php文件。我的formData函数如下:

<script>
    function formData() {
        var serializedValues = jQuery("#msform").serialize();
        var form_data = {
            action: 'ajax_data',
            type: 'post',
            data: serializedValues,
        };
        jQuery.post('mypath/insert.php', form_data); //where data should be sent
        return true;
    }
</script>

在周围搜索时,我试图构建接收数据的php文件,其结构如下:

<?php
if (isset($_POST['data'])) {
    post_things();
    return true;
}
function post_things() {
    $title = trim($_POST['form_title']);
    // where form_title is the input[name] of what I want get, serialised into jquery serializedValues variable
    //other similar inputs
    //do something with $title and other $variables
}
?>

我已经初始化了验证和ajax函数,具体操作如下:

<script>
    $(document).ready(function () {
        msform_init(); //this validate form step by step (it's working!)
        $('#msform').submit(function (event) {
            if (form_completeCheck() && true) { //This check if something empty
                formData();
                if (formData() && true) {
                    window.location.replace("//some redirection to success");
                } else {
                    window.location.replace("//some redirection to failure");
                }
            } else {
                event.preventDefault();
            }
        })
    })
</script>

问题是,当我点击提交时,我被重定向到一个url为mypath的页面?ALL_MY_DATA_serialized。我的错误在哪里?由于我的无知,我看不见它。是在jquery/ajax函数中,在php文件中还是在我的html中?提前感谢您的帮助。

您只需要在事件侦听器的顶部执行event.preventDefault()

$('#msform').submit(function(event){
  event.preventDefault();        
  if(form_completeCheck() && true){ //This check if something empty
    formData();
    if(formData() && true){
        window.location.replace("//some redirection to success");
    } else {
        window.location.replace("//some redirection to failure");
    }
  }
})

HTML脚本

<form id="msform" enctype="multipart/form-data">
    <input type="hidden" name="action" value="do_action"/>
    <fieldset id="publish1" data-check-id="1">
    //some inputs
    </fieldset>
    <fieldset id="publish2" data-check-id="2">
    //some inputs
    </fieldset>
    <fieldset id="publish3" data-check-id="3">
    //some inputs
    </fieldset>
    <fieldset id="publish4" data-check-id="4">
    <input type="button" id="do_action" class="submit action-button pull-right top-35" value="Publish"/>
    </fieldset>
</form>

JavaScript

$("#do_action").click(function(){
    $.ajax({
        type:'POST',
        data:$("#msform").serialize();
        url:'<<php script url>>',
        success:function(data){
            alert(data);
        }
    });
});

php脚本

<?php
    if(isset($_POST['action'])){
        post_things($_POST);
        return true;
    }
    function post_things($request){
        echo "<pre>";
        print_r($request);
        echo "</pre>";
    } 
?>

您被重定向的原因是因为您正在提交表单。

由于在您的<form id="msform" enctype="multipart/form-data">中您没有定义任何操作,因此它将提交给自己。

必须使用preventDefault()阻止表单提交。

 $('#msform').submit(function(event){
   event.preventDefault(); //Add this line
   ..............