通过AJAX逐步提交表单

时间:2014-01-11 15:14:03

标签: php ajax forms

我在PHP文件中有一个如下所示的表单(在Wordpress插件中)。表单也在同一个文件中处理。

我想在“步骤”中保存此表单。例如,用户将在第1页上选择4-5个选项,然后我希望AJAX发布选项。然后用户进入第2页,再次保存选项等。最后一页将有最终提交的CAPTCHA(如果这甚至很重要,我不知道在这个特定情况下)。

我想将整个界面用作单个表单,然后使用jQuery隐藏/显示<div>内的每个页面。

我想知道:

1)如何使用AJAX为这种特定类型的表单提交用户选择。

2)在“步骤”中浏览此表单的最有效方法是什么?

3)我还希望每个页面在表单验证时独立运行。有可能这样做吗?

            <form id="quotation_form" name="form" action="#" method="post">
              <div id="page1">
              <div id="step0_header" class="steps-headers" onClick="step0ToggleClick();">Step 1<span style="margin-left:30px;">Sun Exposure</span></div>
                <div id="step0" class="quotation-steps"><strong>Describe the sun exposure of your planter...</strong><br/>
                  <div class="radio-container"><input type="radio" name="sun_exposure" id="sun_exposure_sunny_most" value="sunny_most"<?php checked( 'sunny_most',$_POST['sun_exposure']); ?> /><label class="label-quotation-steps">Sunny most of the day</label><br class="clear"/></div>
                  <div class="radio-container"><input type="radio" name="sun_exposure" id="sun_exposure_shady_morning" value="shady_morning"<?php checked( 'shady_morning',$_POST['sun_exposure']); ?> /><label class="label-quotation-steps">Shady Morning (Sunny Afternoon)</label><br class="clear"/></div>
                  <div class="radio-container"><input type="radio" name="sun_exposure" id="sun_exposure_sunny_morning" value="sunny_morning"<?php checked( 'sunny_morning',$_POST['sun_exposure']); ?> /><label class="label-quotation-steps">Sunny Morning (Shady Afternoon)</label><br class="clear"/></div>
                  <div class="radio-container"><input type="radio" name="sun_exposure" id="sun_exposure_shady_most" value="shady_most"<?php checked( 'shady_most',$_POST['sun_exposure']); ?> /><label class="label-quotation-steps">Shady most of the day</label><br class="clear"/><br class="clear"/></div>
                </div>

                <div id="step1_header" class="steps-headers" onClick="step1ToggleClick();">Step 2<span style="margin-left:30px;">Height</span></div>
                  <div id="step1" class="quotation-steps"><strong>What is the height of your planter?</strong><br /> 
                    <div class="radio-container"><input type="radio" name="height" id="height_1" value="height_1"<?php checked( 'height_1',$_POST['height']); ?>/><label class="label-quotation-steps">8 to 14 inches</label><br class="clear"/></div>
                    <div class="radio-container"><input type="radio" name="height" id="height_2" value="height_2"<?php checked( 'height_2',$_POST['height']); ?>/><label class="label-quotation-steps">15 to 20 inches</label><br class="clear"/></div>
                    <div class="radio-container"><input type="radio" name="height" id="height_3" value="height_3"<?php checked( 'height_3',$_POST['height']); ?>/><label class="label-quotation-steps">21 inches or more</label><br class="clear"/><br class="clear"/></div>
                  </div>

                  <input type="button" name="get_quote" id="get_quote" value="Save choices and continue..." class="request_quotation_btn" />
                  <input type="hidden" name="submit" value="true" />
               </div> <!-- #page1 -->

               <div id="page2">
                 <!-- Page2 stuff will go here, and is located in this same form. -->
               </div>
             </form>

1 个答案:

答案 0 :(得分:0)

我最近偶然发现了一些实现表单向导的简洁代码。 (所以步骤:1,2,3) http://www.jankoatwarpspeed.com/turn-any-webform-into-a-powerful-wizard-with-jquery-formtowizard-plugin/

看看演示的源代码。实际的库名为formToWizard.js

  1. 使用此http://malsup.com/jquery/form/插件。 (当然,你也可以使用普通的js)

  2. 这也是由图书馆提供的。 (#link 1)

  3. 取决于你的 验证机制。