多步形式php和jquery

时间:2013-11-18 05:33:49

标签: php forms request multi-step

您好我正在使用步骤表单step1,step2等

这里我应用jquery并转到下一页但我的问题是我没有得到下一步请求中第一步的数据。这里只是sinlge页面上的步骤,它们只是显示隐藏和阻止。我应该怎样做才能获得php请求所有步骤数据直到最后一步完成

<form>
<div class="step1">
<input type="text" name="firstname">
</div>

<div class="step2">
<input type="text" name="lastname">
<div>

<input type="submit" name="sub">
<input type="button" name="next">
<input type="button" name"previous">

</form>

这里

2 个答案:

答案 0 :(得分:0)

这取决于你的php实现,但你有两个选择:

1-使用ajax在每个步骤后将数据发送回服务器。 2-每个步骤使用单独的页面。

答案 1 :(得分:0)

点击第1步中的按钮,

  1. 您插入相应的表格并获取插入的ID

  2. 将插入的id值保存在表单中的某些隐藏类型中。

  3. 点击第2步中的按钮,

    1. 您需要使用您在表单中保存的ID更新插入的表格行数据中的相应列及其值
    2. 继续相同的过程,直到完成所有步骤。

          <script>
              $(document).ready(function() {
                 $("#next").click(function() {
                   var currentStep = $('#currentStep').val(); // based on the step you need use some switch or conditional block to pass data and store it accordingly.
                  // insertedId ==0 means you need to insert into table, if it not you need update the column in table
      
                    var formdata = {insertedId:$("#insertedId").val(),currentStep:currentStep, first:$("#first").val()};
      
                     $.ajax({
                       type: "POST",
                       url: "form.php",
                       dataType:'json',
                       data: formdata
                       })
                       .done(function( data ) {
      
                           var NextStep =parseInt(currentStep)+1;
                           $("#currentStep").val(NextStep);
      
                           $("#insertedId").val(data.insertedId);
                           $("#step"+currentStep).hide();
                           $("#step"+NextStep).show();
      
                       });
                  });         
              });
      
      
          </script>
      

      HTML:

              <div id="step1">Step 1:
      
                  <input type="text" name="first" id="first" value="">
      
              </div>
      
               <div id="step2" style="display:none;">Step 2:
      
                  <input type="text" name="last" id="last" value="">
      
              </div>
      
              <input type="hidden" name="insertedId" id="insertedId">
              <input type="hidden" name="currentStep" id="currentStep" value="1">
              <button id="next">Next</button>