PHP foreach循环,并在一个形式addmore按钮 [英] php foreach loop and addmore button in a form

查看:104
本文介绍了PHP foreach循环,并在一个形式addmore按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我使用的形式添加的经验在那里我有一个添加更多按钮,增加了更多的(克隆)的内容和用户获得多一个字段添加用户体验

我用这code来实现这个

 < D​​IV ID =append_palllsjjs>< D​​IV CLASS =full_exp_9092kID ='duplicater'>
                    < D​​IV CLASS =full_one_row_009so>
                < D​​IV CLASS =obe_left_dibbhsy78>
                            < D​​IV CLASS =header_009sos00dd_d>
                        公司名称:LT;跨度> * LT; / SPAN>
                    < / DIV>
                    < D​​IV CLASS =maind_TAxefst67s77s>
                        <输入类型=文本名称=COMP []要求的占位符=公司名称级='cname_990s_EXp/&G​​T;
                    < / DIV>
                        < / DIV>< D​​IV CLASS =obe_left_dibbhsy78>
                            < D​​IV CLASS =header_009sos00dd_d>
                        部门名称<跨度> * LT; / SPAN>
                    < / DIV>
                    < D​​IV CLASS =maind_TAxefst67s77s>
                        <输入类型=TEXTNAME =dept的[]要求的占位符=部门名称级='cname_990s_EXp/&G​​T;
                    < / DIV>
                        < / DIV>
               < / DIV>< D​​IV CLASS =full_one_row_009so>
                < D​​IV CLASS =obe_left_dibbhsy78>
                            < D​​IV CLASS =header_009sos00dd_d>
                        从日期和LT;跨度> * LT; / SPAN>
                    < / DIV>
                    < D​​IV CLASS =maind_TAxefst67s77s>
                         <输入类型=日期数据初始天=1数据初始一年=2011的数据最初一个月=9类='TEx_About_allihhNAME =exsdate []要求/&GT ;
                    < / DIV>
                        < / DIV>< D​​IV CLASS =obe_left_dibbhsy78>
                            < D​​IV CLASS =header_009sos00dd_d>
                        到Date<跨度> * LT; / SPAN>
                    < / DIV>
                    < D​​IV CLASS =maind_TAxefst67s77s>
                          <输入类型=日期数据初始天=1数据初始一年=2012的数据最初一个月=10级='TEx_About_allihhNAME =exedate []要求/&GT ;
                    < / DIV>
                        < / DIV>
               < / DIV>< D​​IV CLASS =full_one_row_009so>
                < D​​IV CLASS =obe_left_dibbhsy78>
                            < D​​IV CLASS =header_009sos00dd_d>
                        简介<跨度> * LT; / SPAN>
                    < / DIV>
                    < D​​IV CLASS =maind_TAxefst67s77s>
                        <输入类型=文本名称=简介[]要求的占位符=档案类='cname_990s_EXp/&G​​T;
                    < / DIV>
                        < / DIV>< D​​IV CLASS =obe_left_dibbhsy78>
               < D​​IV CLASS =header_009sos00dd_d>
                                       < / DIV>
                            <输入类型=按钮NAME =addmoreVALUE =添加更多级=按钮小白的onclick ='重复();/>
                        < / DIV>
               < / DIV>
                < / DIV>< / DIV>

JS

  VAR I = 0;
  VAR原=的document.getElementById('duplicater'); 功能重复的(){
VAR克隆= original.cloneNode(真); //深克隆
clone.id =duplicetor+ + +我; //只能有一个ID一个元素
 original.parentNode.appendChild(克隆);
 }

在这里,我想新字段添加时应该是空的(现在它显示在文本框pre填充的值相同的内容)

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

PHP

  $谱曲= $ _ POST ['补偿']​​;
    $剖面= $ _ POST ['个人资料'];
    $ exedate = $ _ POST ['exedate'];
    $ exsdate = $ _ POST ['exsdate'];
    $部门= $ _ POST ['部门'];
    如果(空($谱曲)||空($配置文件)||空($ exedate)||空($ exsdate)||空($ DEPT)){
        回声请填写所有标有*的';模具;
    }
    的foreach($谱曲为$值){
        //插入表名(字段1,场2,FIELD3,...)VALUES('COMP1,DEP1,配置文件1....)

//插入许多精密组件作为没有数组中的元素
        }

请建议我用这个PHP code如何使用foreach循环,这样我可以在另一个数组corrosponging值插入的行数无数组中元素的

pleaes注意,这个问题有书面请随时帮助任何问题的两个问题。

一个是问心无愧PHP和哪吒使用Ajax


解决方案

使用以下code到清晰的克隆形式:


  

请注意:必须在文档添加的jQuery文件


 <脚本SRC =HTTP://$c$c.jquery.com/jquery-1.11.3.min.js>< / SCRIPT>
<脚本>
 变种I = 0;
 VAR原=的document.getElementById('duplicater');  功能重复的(){
    VAR克隆= original.cloneNode(真); //深克隆
    I = ++我;
    clone.id =duplicetor+我; //只能有一个ID一个元素
    original.parentNode.appendChild(克隆);
    clearCloneForm(clone.id);
  }  功能clearCloneForm(ID){
    VAR DIVID ='#'+ ID;
    $(DIVID).find(输入[键入^ ='文本'],输入[键入^ ='日期'])。每个(函数(){
        $(本).VAL('');
    });
  }
< / SCRIPT>

hello i am using a form to add experience to users where i have a add more button which adds more (clones) the content and users get one more field to add experience

i am using this code to achieve this

<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);
 } 

here i want the new fields when added should be empty (right now it is showing the same content with pre filled values in textbox )

second issue is i want to insert the data in table for each value of the array i know this can be donr by foreach loop

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'....)

// insert as many feilds as the no of elements in the array }

please suggest me with this php code how to use the foreach loop so that i can insert as many rows as the no of elements in the array with corrosponging values in another array

pleaes note that this question has two questions written please feel free to help for any of the question.

one is wth php and anothr with ajax

解决方案

Use following code to clear Cloned form :

NOTE : Must add jquery file in document

<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>

这篇关于PHP foreach循环,并在一个形式addmore按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆