表格

时间:2015-12-05 05:57:22

标签: javascript php mysql arrays foreach

你好我正在使用一个表单为用户添加经验,我有一个添加更多按钮,可以添加更多(克隆)内容,用户可以再添加一个字段来增加体验

我正在使用此代码来实现此目的

<div id="append_palllsjjs"><div class="full_exp_9092k" id='duplicater'>
                    <div class="full_one_row_009so">
                <div class="obe_left_dibbhsy78">
                            <div class="header_009sos00dd_d">
                        Company Name <span>*</span>
                    </div>
                    <div class="maind_TAxefst67s77s">
                        <input type="text" name="comp[]" required placeholder="company Name" class='cname_990s_EXp'/>
                    </div>
                        </div><div class="obe_left_dibbhsy78">
                            <div class="header_009sos00dd_d">
                        Department Name <span>*</span>
                    </div>
                    <div class="maind_TAxefst67s77s">
                        <input type="text" name="dept[]" required placeholder="Department Name" class='cname_990s_EXp'/>
                    </div>
                        </div>
               </div><div class="full_one_row_009so">
                <div class="obe_left_dibbhsy78">
                            <div class="header_009sos00dd_d">
                        From Date <span>*</span>
                    </div>
                    <div class="maind_TAxefst67s77s">
                         <input type="date" data-initial-day="1" data-initial-year="2011" data-initial-month="9" class='TEx_About_allihh' name="exsdate[]" required/>
                    </div>
                        </div><div class="obe_left_dibbhsy78">
                            <div class="header_009sos00dd_d">
                        To Date <span>*</span>
                    </div>
                    <div class="maind_TAxefst67s77s">
                          <input type="date" data-initial-day="1" data-initial-year="2012" data-initial-month="10" class='TEx_About_allihh' name="exedate[]" required/>
                    </div>
                        </div>
               </div><div class="full_one_row_009so">
                <div class="obe_left_dibbhsy78">
                            <div class="header_009sos00dd_d">
                        Profile <span>*</span>
                    </div>
                    <div class="maind_TAxefst67s77s">
                        <input type="text" name="profile[]" required placeholder="Profile" class='cname_990s_EXp'/>
                    </div>
                        </div><div class="obe_left_dibbhsy78">
               <div class="header_009sos00dd_d">
                                       </div>
                            <input type="button" name="addmore" value="Add More" class='button small white' onclick='duplicate();'/>
                        </div>
               </div>
                </div></div>

JS

  var i = 0;
  var original = document.getElementById('duplicater');

 function duplicate() {
var clone = original.cloneNode(true); // "deep" clone
clone.id = "duplicetor" + ++i; // there can only be one element with  an ID
 original.parentNode.appendChild(clone);
 } 

这里我想要添加的新字段应为空(现在它显示与文本框中预填充值相同的内容)

第二个问题是我想在表中为数组的每个值插入数据我知道这可以通过foreach循环来代替

PHP

 $comps=$_POST['comp'];
    $profile=$_POST['profile'];
    $exedate=$_POST['exedate'];
    $exsdate=$_POST['exsdate'];
    $dept=$_POST['dept'];
    if(empty($comps) || empty($profile) || empty($exedate) || empty($exsdate) || empty($dept) ){
        echo 'Please Fill all the fields marked with *';die;
    }
    foreach($comps as $value){
        // insert into tablename (field1,field2,field3,...) values ('comp1','dep1','profile1'....)

//插入与数组中元素的数量一样多的字段         }

请建议我使用这个PHP代码如何使用foreach循环,以便我可以插入与数组中的元素no一样多的行与另一个数组中的corrosponging值

请注意,这个问题有两个问题,请随时为任何问题提供帮助。

一个是php和anothr with ajax

2 个答案:

答案 0 :(得分:2)

使用以下代码清除克隆表单:

  

注意:必须在文档中添加jquery文件

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
 var i = 0;
 var original = document.getElementById('duplicater');

  function duplicate(){ 
    var clone = original.cloneNode(true); // "deep" clone
    i = ++i;
    clone.id = "duplicetor"+ i; // there can only be one element with  an ID
    original.parentNode.appendChild(clone);
    clearCloneForm(clone.id);
  }

  function clearCloneForm(id){ 
    var divId = '#'+id;
    $(divId).find("input[type^='text'], input[type^='date']").each(function() {
        $(this).val('');
    }); 
  }
</script>

答案 1 :(得分:1)

以下是符合您新要求的代码:

  

如果用户想要删除表单块部分用户,请添加删除按钮   很容易:

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
 var i = 0;
 var original = document.getElementById('duplicater');

  function duplicate(){ 
    var clone = original.cloneNode(true); // "deep" clone
    i = ++i;
    clone.id = "duplicetor"+ i; // there can only be one element with  an ID
    original.parentNode.appendChild(clone);
    addButton(clone.id,i);
    clearCloneForm(clone.id);
  }

  function clearCloneForm(id){ 
    var divId = '#'+id;
    $(divId).find("input[type^='text'], input[type^='date']").each(function() {
        $(this).val('');
    });
  }

  function addButton(id,ii){ 
    var divId = '#'+id;
    $(divId).append('<input type="button" value="Remove" class="button small white" id="'+ii+'" onclick="rBlock('+ii+')" />');
  }



function rBlock(ii){
    $('#'+ii).on('click', function(e){ 
        var parentDiv = $(this).parent();
        if(parentDiv.attr('id') !== ii){
            parentDiv.remove();
        }
    });
    $('#'+ii).trigger( "click" );
  }
</script>