【问题标题】:Unable to print the $length variable? Trying to input data from cloned forms无法打印 $length 变量?尝试从克隆的表单中输入数据
【发布时间】:2014-06-25 17:08:11
【问题描述】:

我的代码如下。我在 html 输入标签名称字段、title[]、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>

【问题讨论】:

  • 您可以添加var_dump($_POST['first_name']) 并发布输出吗?我怀疑你的数组中只有一个条目。
  • 此代码只发送一个条目。你的重复在哪里?
  • 是的,我只得到一个条目。我不知道如何重复,请帮助我重复。
  • 我的印象是每次克隆表单时都会运行 foreach。对不起,我是新手
  • @MrGlass 这是我在 vardump($_POST['first_name']) :: array(1) { [0]=> string(8) "firstone" } 之后得到的

标签: javascript php html forms


【解决方案1】:
<?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>

【讨论】:

  • 它在一定程度上起作用,在第一次克隆后,当你第三次尝试时,它会给出 6 个克隆形式而不是一个克隆
  • 我的错误,我只是克隆它。我将对其进行编辑并对此进行修复。
  • @AryaSingh 现在检查没问题。我修复了添加表单,并进行检查,如果页面上只有一个表单,您将无法删除表单。
  • 非常感谢。你太棒了。这对我帮助很大。编辑后工作正常。谢谢。
猜你喜欢
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
  • 2020-05-17
  • 2012-08-14
  • 1970-01-01
  • 1970-01-01
  • 2020-10-25
  • 1970-01-01
相关资源
最近更新 更多