多页表单提交

Bri*_*lph 4 html javascript php forms jquery

我有一个问题,似乎无法通过我的方式.

首先,我有一个由专业人士建立的网站,我们不再与该公司建立工作关系.我现在自己管理这个网站.我有能力,但我绝不是一位经验丰富的网络开发人员.

背景:我们有一个应用程序,它使用呈现给最终用户的多页表单.表单以7个步骤呈现,但它都是从一个php文件完成的,使用(我认为)jquery/javascript来循环执行这些步骤,并验证一些字段.在最后一步中,将提供摘要供用户提交.这很好用.

以下是我认为处理页面循环的相关javascript:

<script>
$(function () {

    window.confirmLeave = true;

    $('.datefield').datepicker();

    var cache = {};                                                                                             // caching inputs for the visited steps

    $("#appForm").bind("step_shown", function(event,data){  

         if(data.isLastStep){                                                                                      // if this is the last step...then
                $("#summaryContainer").empty();                                                                     // empty the container holding the 
                $.each(data.activatedSteps, function(i, id){                                                        // for each of the activated steps...do
                    if(id === "summary") return;                                                                    // if it is the summary page then just return
                    cache[id] = $("#" + id).find(".input");                                                         // else, find the div:s with class="input" and cache them with a key equal to the current step id
                    cache[id].detach().appendTo('#summaryContainer').show().find(":input").removeAttr("disabled");  // detach the cached inputs and append them to the summary container, also show and enable them
                });
            }else if(data.previousStep === "summary"){                                                              // if we are movin back from the summary page
                $.each(cache, function(id, inputs){                                                                 // for each of the keys in the cache...do
                    var i = inputs.detach().appendTo("#" + id).find(":input");                                      // put the input divs back into their normal step
                    if(id === data.currentStep){                                                                    // (we are moving back from the summary page so...) if enable inputs on the current step
                         i.removeAttr("disabled");
                    }else{                                                                                          // disable the inputs on the rest of the steps
                        i.attr("disabled","disabled");
                    }
                });
                cache = {};                                                                                         // empty the cache again
            }
        });

</script>
Run Code Online (Sandbox Code Playgroud)

我还包括以下表格的html:

<form name="appForm" id="appForm" action="submit-app-exec.php" method="post" 
enctype="multipart/form-data" autocomplete="off" onSubmit="showProgressBar()">

<fieldset class="step" id="page_1">
<div class="input">
<?php include("add-company/step1.html"); ?>
</div>
</fieldset>

<fieldset class="step" id="page_2">
<div class="input">
<?php include("add-company/step2.html"); ?>
</div>
</fieldset>

<fieldset class="step" id="page_3">
<div class="input">
<?php include("add-company/step3.html"); ?>
</div>
</fieldset>

<fieldset class="step" id="page_4">
<div class="input">
<?php include("add-company/step4.html"); ?>
</div>
</fieldset>    

<fieldset class="step" id="page_5">
<div class="input">
<?php include("add-company/step5.html"); ?>
</div>
</fieldset>

<fieldset class="step" id="page_6">
<div class="input">
<?php include("add-company/step6.html"); ?>
</div>
</fieldset>

<fieldset class="step" id="page_7">
<div class="input">
<?php include("add-company/step7.html"); ?>
</div>
</fieldset>

<fieldset class="step" id="summary" >
    <span class="font_normal_07em_black">Summary page</span><br />
    <p>Please verify your information below.</p>
    <div id="summaryContainer"></div>
</fieldset>

<div id="wizardNavigation">

    <button class="navigation_button" onclick="javascript:saveApp()">Save</button>
    <input class="navigation_button" id="back" value="Back" type="reset" />
    <input class="navigation_button" id="next" value="Next" type="submit" onclick="javascript:noSaveApp()" />

    <div class="clearFix"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

加载页面时,每个字段集都有其他类和样式属性:

class="step ui-formwizard-content ui-helper-reset ui-corner-all" style="display: none;"
Run Code Online (Sandbox Code Playgroud)

在这个过程中,我可以用萤火虫看,并看到显示:无; 循环并在与该字段集交互时更改为"阻止".

问题:我们没有以任何方式构建用户保存进度并在以后完成.我现在正试图这样做.我已成功创建了"保存"按钮,该按钮触发javascript以更改表单的操作,该操作将数据发送到处理和处理POST数据到MySQL的新php文件.但是,这有效,POST只传递当前查看的字段集中的数据,而不是POST所有数据.我无法弄清楚如何确保所有表单数据都被POST.任何指导或建议都会有所帮助.谢谢.

编辑:

我能够使用以下内容加载正确的页面:

$(function(){ $('#appForm').formwizard('show','" . $row["current_step"] . "'); }); 
Run Code Online (Sandbox Code Playgroud)

这会加载正确的页面.现在的问题是,最后一步是一个摘要页面,显示最终提交的所有输入元素.但是,它似乎只显示被查看的页面中的元素,我很确定它是数组"data.activatedSteps",用于确定元素是否在最终摘要中显示.你的代码会比我的更好地解决这个问题吗 再次感谢您对此的帮助. -

Dan*_*Dan 5

<input>具有该属性的对象'disable'不会在post/get中返回.

这是在您的问题中需要注意的重要JavaScript.

if(id === data.currentStep){                                                                    // (we are moving back from the summary page so...) if enable inputs on the current step
    i.removeAttr("disabled");
}else{                                                                                          // disable the inputs on the rest of the steps
    i.attr("disabled","disabled");
}
Run Code Online (Sandbox Code Playgroud)

这就是前一个编码器控制用户可以编辑的内容的方式.如果您希望在用户首先点击您的保存按钮时发回所有值

removeAttr("disabled");
Run Code Online (Sandbox Code Playgroud)

从页面上的所有内容然后提交发布/获取.

看起来前一个编码器通过浏览页面上的所有数据并在此处禁用它来完成此操作.

if(data.isLastStep){                                                                                    // if this is the last step...then
    $("#summaryContainer").empty();                                                                     // empty the container holding the 
    $.each(data.activatedSteps, function(i, id){                                                        // for each of the activated steps...do
        if(id === "summary") return;                                                                    // if it is the summary page then just return
        cache[id] = $("#" + id).find(".input");                                                         // else, find the div:s with class="input" and cache them with a key equal to the current step id
        cache[id].detach().appendTo('#summaryContainer').show().find(":input").removeAttr("disabled");  // detach the cached inputs and append them to the summary container, also show and enable them
    });
}
Run Code Online (Sandbox Code Playgroud)

因此,您可以在发布返回时调用该代码,也可以执行类似的操作以启用页面上的输入.

编辑:

根据您的评论,我建议使用它来解决您的问题,以便您始终使用相同的实用程序和库来操纵页面上的对象.加上JQuery只是方便.

$(':input.ui-wizard-content').each(function() {$( this ).removeAttr("disabled");});
Run Code Online (Sandbox Code Playgroud)

:input获取页面上的所有输入..ui-wizard-content告诉JQuery只查找该类的输入,然后.each遍历function() {$( this ).removeAttr("disabled");}在找到的对象上运行lambda函数()的每个对象.

编辑2:

很难说出$row[]代码中的内容.它显然是一个容器,但你提供的代码有点脱离背景,所以我无法确定它是什么.

话虽如此,如果我只使用DOM(页面上的Java脚本表示),我知道你之前发布的功能,我会做这样的事情.

更换

if(data.isLastStep){                                                                                    // if this is the last step...then
    $("#summaryContainer").empty();                                                                     // empty the container holding the 
    $.each(data.activatedSteps, function(i, id){                                                        // for each of the activated steps...do
        if(id === "summary") return;                                                                    // if it is the summary page then just return
        cache[id] = $("#" + id).find(".input");                                                         // else, find the div:s with class="input" and cache them with a key equal to the current step id
        cache[id].detach().appendTo('#summaryContainer').show().find(":input").removeAttr("disabled");  // detach the cached inputs and append them to the summary container, also show and enable them
    });
}
Run Code Online (Sandbox Code Playgroud)

if(data.isLastStep){ 
    $("#summaryContainer").empty();
    $('.input :input').detach().appendTo('#summaryContainer').show().find(":input").removeAttr("disabled");
}
Run Code Online (Sandbox Code Playgroud)

这主要是借用了之前的编码器所做的事情.他假设表格中的所有内容都会data.activatedSteps因为他创建的工作流而进入,但现在不再是这样了.因此,您需要查看他在何处添加数据data.activatedSteps以及在重新加载时将保存的内容以相同的方式放入其中.或者只是使用我忽略所有data内容的行,然后在类中抓取每个<input>对象input并将其添加到容器中.

注意:JQuery $(TEXT)使用与CSS相同的对象定义语法,使用一些特别易用的添加内容,如':input'.有一个很好的参考什么我谈论这里的CSS参考和JQuery的特殊选择语法的检查在这里.这就是我如何用一个非常紧凑的语句来编写能够抓住你感兴趣的东西的东西.

  • @BrianRandolph你写的是完全有效的Javascript,但考虑到你的前任使用的是JQuery,我建议像这样的`$('ui-wizard-content').each(function(){$(this).removeAttr("disabled ");});`因为你正在使用相同的库和函数,因为我真的很喜欢JQuery.另外看1行就可以了.:) (2认同)