无法打印$length变量?试图从克隆表单输入数据

Unable to print the $length variable? Trying to input data from cloned forms

本文关键字:表单 数据 输入 打印 length 变量      更新时间:2023-09-26

我的代码如下:我有数组声明在html输入标签名称字段,标题[],first_name[], last_name[], email_address[], twitter_handle[]。为了在.js文件中进行表单克隆,我已经声明了它们所有的数组。现在,我正试图从表单输入到数据库的数据。我使用foreach来管理数组,我还使用了一个$length变量,这将给我的形式克隆工作的数量。无论复制多少次表单,都只打印第一个表单。请帮助。我的代码如下:

    if(isset($_POST['title']) && isset($_POST['first_name']) && isset($_POST['last_name']) && isset($_POST['email_address']) && isset($_POST['twitter_handle'])){
    $length=0;
   foreach($_POST['title'] as $key=>$value){
      $title[$key]=$value;
      $length=$length + 1;
      echo '$key: '.$key.' '.$length;
      //print_r($value);
    }    
    foreach($_POST['first_name'] as $key=>$value){
      $first_name[$key]=$value;
    }
    foreach($_POST['last_name'] as $key=>$value){
      $last_name[$key]=$value;
    }  
    foreach($_POST['email_address'] as $key=>$value){
      $email_address[$key]=$value;
    }
    foreach($_POST['twitter_handle'] as $key=>$value){
      $twitter_handle[$key]=$value;
    }
    echo '$lenght : '.$length.' ';
    for($i=0;$i<$length;$i++){
      print_r($first_name[$i]);
    }     
   }

..................作为参考,这是我的html代码:

    <form action="<?php $_SERVER['PHP_SELF']?>" method="POST" id="sign-up_area" role="form">
<label class="label_ttl control-label" for="title">Title:</label>
        <div class="form-group">
            <select class="select_ttl form-control" name="title[]" id="title">
              <option value="" selected="selected" disabled="disabled">Select your title</option>
              <option value="Dr.">Dr.</option>
              <option value="Mr.">Mr.</option>
              <option value="Mrs.">Mrs.</option>
              <option value="Ms.">Ms.</option>
              <option value="Sir">Sir</option>
            </select> 
          </div>
        <!-- Text input-->
        <div class="form-group">
          <label class="label_fn control-label" for="first_name">First name:</label>
          <input id="first_name" name="first_name[]" type="text" placeholder="" class="input_fn form-control" required>
          <p class="help-block">This field is required.</p>
        </div>
        <!-- Text input-->
        <div class="form-group">
          <label class="label_ln control-label" for="last_name">Last name:</label>
          <input id="last_name" name="last_name[]" type="text" placeholder="" class="input_ln form-control">
        </div>
         <div class="form-group">
          <label class="label_email control-label" for="email_address">Email:</label>
          <input id="email_address" name="email_address[]" type="text" placeholder="example@example.com" class="input_email form-control">
        </div>
        <!-- Prepended text-->
        <label class="label_twt control-label" for="institution">Enter Institution / Organization:</label>
        <div class="input-group form-group">
          <input id="twitter_handle" name="twitter_handle[]" class="input_twt form-control" placeholder="" type="text">
        </div>
        <!-- Text input-->

        </div><!-- end #entry1 -->
        <!-- Button (Double) -->
        <p>
        <button type="button" id="btnAdd" name="btnAdd" class="btn btn-info">add section</button>
          <button type="button" id="btnDel" name="btnDel" class="btn btn-danger">remove section above</button>
        </p>


        <!-- Button -->
        <p>
          <button id="submit_button" name="submit_button" class="btn btn-primary">Submit</button>
        </p>
        </fieldset>
        </form>
<?php
 if(isset($_POST['title']) && isset($_POST['first_name']) && isset($_POST['last_name']) && isset($_POST['email_address']) && isset($_POST['twitter_handle'])){
  $length=0;
  foreach($_POST['title'] as $key=>$value){
  $title[$key]=$value;
  $length = $length + 1;
  //echo '$key: '.$key.' '.$length;
  //print_r($value);
}    
foreach($_POST['first_name'] as $key=>$value){
  $first_name[$key]=$value;
}
foreach($_POST['last_name'] as $key=>$value){
  $last_name[$key]=$value;
}  
foreach($_POST['email_address'] as $key=>$value){
  $email_address[$key]=$value;
}
foreach($_POST['twitter_handle'] as $key=>$value){
  $twitter_handle[$key]=$value;
}
echo '$lenght : '.$length.' ';
for($i=0;$i<$length;$i++){
  echo ($first_name[$i])."<br/>";
}   
   }
    ?>
    <head>
   <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
   </head>
    <form action="<?php $_SERVER['PHP_SELF']?>" method="POST" id="sign-up_area" role="form">
    <div class="form" id="form_1">
    <label class="label_ttl control-label" for="title">Title:</label>
       <div class="form-group">
         <select class="select_ttl form-control" name="title[]" id="title">
          <option value="" selected="selected" disabled="disabled">Select your title</option>
          <option value="Dr.">Dr.</option>
          <option value="Mr.">Mr.</option>
          <option value="Mrs.">Mrs.</option>
          <option value="Ms.">Ms.</option>
          <option value="Sir">Sir</option>
        </select> 
        </div>
    <!-- Text input-->
    <div class="form-group">
      <label class="label_fn control-label" for="first_name">First name:</label>
      <input id="first_name" name="first_name[]" type="text" placeholder="" class="input_fn form-control" required>
      <p class="help-block">This field is required.</p>
    </div>
    <!-- Text input-->
    <div class="form-group">
      <label class="label_ln control-label" for="last_name">Last name:</label>
      <input id="last_name" name="last_name[]" type="text" placeholder="" class="input_ln form-control">
    </div>
     <div class="form-group">
      <label class="label_email control-label" for="email_address">Email:</label>
      <input id="email_address" name="email_address[]" type="text" placeholder="example@example.com" class="input_email form-control">
    </div>
    <!-- Prepended text-->
    <label class="label_twt control-label" for="institution">Enter Institution / Organization:</label>
    <div class="input-group form-group">
      <input id="twitter_handle" name="twitter_handle[]" class="input_twt form-control" placeholder="" type="text">
    </div>
    <!-- Text input-->

    </div><!-- end #entry1 -->
    <!-- Button (Double) -->
    <p>
    <button type="button" id="btnAdd" name="btnAdd" class="btn btn-info">add section</button>
      <button type="button" id="btnDel" name="btnDel" class="btn btn-danger">remove section above</button>
    </p>


    <!-- Button -->
    <p>
      <button id="submit_button" name="submit_button" class="btn btn-primary">Submit</button>
    </p>
    </fieldset>
    </div>
    </form>
 <script type="text/javascript">
$('#btnAdd').click(function(){
    var form_length = $(".form").length;
    $('#form_'+form_length).clone().prop({ id: "form_"+parseInt(form_length+1)}).insertAfter('#form_'+form_length);
});
$('#btnDel').click(function(){
    var form_length = $(".form").length;
    if(form_length == 1){
        alert("One form must exist!");
    }else{
        $('.form').last().remove();         
    }
});
</script>