weixin_33725239 2015-07-30 15:37 采纳率: 0%
浏览 16

表单标签过早关闭

As far as I know this means the html code is invalid, usually because of incorrect nesting.

This form is generated on a button press by echoing from a php function called with jQuery.ajax():

<?php

echo '
<div class="row survey-container text-center">
    <form id="surveyForm" action="processSurvey.php" method="post">

        <h3>Alimentos</h3>
        <h4>Sabor</h4>
        <div class="form-group">';
            for ($i = 0; $i <= 10; $i++) {  // Imprime lista de sabor
                echo '
                    <label class="radio-inline">
                        <input type="radio" name="sabor" id="saborRadio'. $i .'" value="'. $i .'">'. $i.'
                    </label>
                ';
            }
            echo '
        </div>
        <div class="form-group">
            <button class="btn btn-default surveyForm-btn" type="submit">Enviar</button>
        </div>

    </form>
</div>
';
?>

All code behaves as expected but one line:

[...] 
        <form id="surveyForm" action="processSurvey.php" method="post"></form>
[...]

The form is closing itself as soon as it opens. Am I breaking some rule? Can't seem to find the problem. I don't think the issue is on the js but I'll include it

        $('body').on('submit', '#surveyForm', function(e){
        console.log("Clicked on #surveyForm ");
        var data = $(this).serialize();
        console.log( data );
        $.ajax({
            method: "POST",
            url: "processSurvey.php", 
            data: data, 
            success: function(result){
                console.log("Ajax call to processSurvey success");
                $("#surveyForm").clearForm();
                console.log(result);
                console.log( data );

            } 
        });
        return false;
    });

Console logs indicate everything is all right but no data is being sent (empty array), which makes sence considering the form is closing itself prematurely.

EDIT: I think I know what the problem is, the form is being created inside a table, which is invalid html (right?) I'll post an update when I fix that. Should I mark as answered?

  • 写回答

2条回答 默认 最新

  • local-host 2015-07-30 15:42
    关注

    You are serializing the button, not the form.

    var data = $(this).closest("form").serialize();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧