dragon8899 2016-10-18 18:40
浏览 27
已采纳

在PHP中记录动态输入值

I have quite a large form, several pages of which make use of some JS to dynamically add inputs whenever the user clicks a plus button, allowing them to add in extra jobs and what have you.

I have that portion of the form working. My problem comes in trying to capture the dynamic fields in the PHP document. I seem to only be getting the first values that are input into the form, and nothing from the dynamically added inputs.

What I have Tried

I've separated one of the pages of the form that has two such inputs and tried capturing the first level of inputs in PHP. This is working properly.

What I am Unsure Of:

How do I create variables in PHP or the potentiality of variables so I can grab them later in php?

I've recreated the example here, but only the first question allows for dynamic inputs. I just included the one js file:

http://codepen.io/theodore_steiner/pen/xEyzNm?editors=1010

HTML:
 <form action="putToToCSV.php" method="post" class="scholarshipForm">

                <div class="input-group" id="unit-level-involvement">
                    <label id="unitInvolvement">Unit Involvement *</label>
                    <div id="unitLevelInvolvement">
                        <input type="text" class="two-lines-textbox" name="unitLevelPosition[1]" placeholder="Position/Committee" onBlur="this.placeholder='Position/Committee'" onFocus="this.placeholder=''" onKeyUp="checkPage3()" />
                        <input type="text" class="two-lines-textbox" name="oectaUnit_1" id="oectaUnit_1" placeholder="Unit" onBlur="this.placeholder='Unit'" onFocus="this.placeholder=''" onKeyUp="checkPage3()" />

                        <div class="clearFix"></div>

                        <input type="text" class="two-lines-textbox" name="unitPresident_1" id="unitPresident_1" placeholder="Unit President" onBlur="this.placeholder='Unit President'" onFocus="this.placeholder=''" onKeyUp="checkPage3()" />
                        <input type="date" class="two-lines-textbox" name="unitYear_1" id="unitYear_1" onKeyUp="checkPage3()" />
                        <input type="button" value="+" onClick="addUnitInvolvement()" />
                    </div>
                 </div><!-- end of unit-level-involvement div-->

    </form><!--endForm-->

JS:

var i = 0;
function addUnitInvolvement()
{
    i++;
    var unitInvolvementDiv = document.createElement("div");
    unitInvolvementDiv.innerHTML = '<input type="text" class="four-lines" name="unitLevelPosition_'+i+'" placeholder="Position/Committee">  <input type="text" class="four-lines" name="oectaUnit_'+i+'" placeholder="Unit"> <input type="text" class="four-lines" name="unitPresident_'+i+'" placeholder="Unit President"> <input type="date" class="four-lines" name="unitYear_'+i+'"> <input type="button" value="-" onclick="removeUnitInvolvement(this)">';

    document.getElementById("unitLevelInvolvement").appendChild(unitInvolvementDiv);
}

function removeUnitInvolvement(unitInvolvementDiv)
{
    document.getElementById("unitLevelInvolvement").removeChild(unitInvolvementDiv.parentNode);
};

PHP:

<?php


$unitLevelPosition_1 = $_POST["unitLevelPosition_1"];

$oectaUnit_1 = $_POST["oectaUnit_1"];

$unitPresident_1 = $_POST["unitPresident_1"];

$unitYear_1 = $_POST["unitYear_1"];

$provincialPosition_1 = $_POST["provincialPosition_1"];

$provincialDate_1 = $_POST["provincialDate_1"];

$piText_1 = $_POST["piText_1"];


echo $unitLevelPosition_1 . "<br>" . $oectaUnit_1 . "<br>" . $unitPresident_1 . "<br>" . $unitYear_1 . "<br>" . $provincialPosition_1 . "<br>" . $provincialDate_1 . "<br>" . $piText_1;

?>
  • 写回答

1条回答 默认 最新

  • ds3016 2016-10-18 19:04
    关注

    You can use array syntax - e.g. name="unitLevelPosition[]" then when the form is submitted in PHP, you can utilize $_POST['unitLevelPosition'] as an array. I tried searching for reputable resources but haven't found much. However, take a look at this page, as well as this one (I know that pertains to checkboxes but the concept is the same).

    var i = 0;
    function addUnitInvolvement()
    {
        i++;
        var unitInvolvementDiv = document.createElement("div");
        unitInvolvementDiv.innerHTML = '<input type="text" class="four-lines" name="unitLevelPosition[]" placeholder="Position/Committee">  <input type="text" class="four-lines" name="oectaUnit[]" placeholder="Unit"> <input type="text" class="four-lines" name="unitPresident[]" placeholder="Unit President"> <input type="date" class="four-lines" name="unitYear[]"> <input type="button" value="-" onclick="removeUnitInvolvement(this)">';
    
        document.getElementById("unitLevelInvolvement").appendChild(unitInvolvementDiv);
    }
    
    function removeUnitInvolvement(unitInvolvementDiv)
    {
        document.getElementById("unitLevelInvolvement").removeChild(unitInvolvementDiv.parentNode);
    };
    <form action="putToToCSV.php" method="post" class="scholarshipForm">
    
                    <div class="input-group" id="unit-level-involvement">
                        <label id="unitInvolvement">Unit Involvement *</label>
                        <div id="unitLevelInvolvement">
                            <input type="text" class="two-lines-textbox" name="unitLevelPosition[]" placeholder="Position/Committee" onBlur="this.placeholder='Position/Committee'" onFocus="this.placeholder=''" onKeyUp="checkPage3()" />
                            <input type="text" class="two-lines-textbox" name="oectaUnit[]" id="oectaUnit_1" placeholder="Unit" onBlur="this.placeholder='Unit'" onFocus="this.placeholder=''" onKeyUp="checkPage3()" />
    
                            <div class="clearFix"></div>
    
                            <input type="text" class="two-lines-textbox" name="unitPresident[]" id="unitPresident_1" placeholder="Unit President" onBlur="this.placeholder='Unit President'" onFocus="this.placeholder=''" onKeyUp="checkPage3()" />
                            <input type="date" class="two-lines-textbox" name="unitYear_1" id="unitYear_1" onKeyUp="checkPage3()" />
                            <input type="button" value="+" onClick="addUnitInvolvement()" />
                        </div>
                     </div><!-- end of unit-level-involvement div-->
    
        </form><!--endForm-->

    For an example, see this phpfiddle - you can see the PHP code to start, and press F9 or click the button labeled Run - F9 to run the code. After clicking the button labeled Submit form you should see the output of processing the data in $_POST.

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值