简体   繁体   English

回声在PHP中为循环创建的文本框的值

[英]Echo out values of text-boxes created fia for loop in PHP

I have the following form: (please note I am aware that there are no validation in my code) 我有以下表格:(请注意我知道我的代码中没有验证)

<form name="frmsplitter" method="POST" enctype="multipart/form-data" action = 'step2.php'>

        <table>
            <thead>
                <tr>

                    <select style="width: 80%" name="cobbulkamount">
                        <option>please select a bulk item</option>
                        <option value= 100>100</option>
                        <option value= 200>200</option>
                        </select>

                </tr>
            </thead>
            <tbody>
                <tr>
                    <td><label>Select number of items you want the bulk amount to be splited : </label><input type="text" name="txtnumberofitems" value="<?php if (isset($_POST['btnsplitItem'])){echo $_POST['txtnumberofitems'];}?>" /></td>
                    <td></td>
                </tr>
                <tr>

                    <td><input type="submit" value="submit" name="btnsplitItem" /></td>
                </tr>
            </tbody>
        </table>           
    </form>`

the form submits it goes to the following page : 表单提交它转到以下页面:

    <?php

    if (!empty($_POST['txtnumberofitems'])) {
        $bulkvalue = 200;
        $numberofitems = $_POST['txtnumberofitems'];
            echo "<form name='frmsplitter'  method='POST' action = 'step3.php'>";     
            echo "amount to be splited <input type='text' name='txtamountof' value =$numberofitems> ";
                for ($index = 0; $index < $numberofitems; $index++) {           
                    echo  "<table>".  
                    "<tr>
                    <td>|amount: <input type='text' name='txtamount.$index'/></td>
                    <td>|Reference 2: <input type='text' name='txtref.$index'/></td>
                    <td>|DCIP:<select name='cobdcip'.$index>
                    <option>DR</option>
                    <option>CR</option>
                    </select></td>
                    </tr>
                    </table>";
                    }
                        echo "<br>";
                        echo "<input type='submit' name='btnpopulate' />";
                        echo "</form>";        
    }       
    ?>`

the form submits it goes to the following page : 表单提交它转到以下页面:

        if (isset($_POST['btnpopulate'])) {
            $number = (int)$_POST['txtamountof'];
            for ($index = 0; $index < $number; $index++) {
                if (!empty($_POST['txtamount'.$index])) 
            { 
                    echo 'nooooooooooo';
            }
                else {                     
                        echo $_POST['txtamount'.$index];
                        echo '<br>';
                }   
            }
        } `

My problem is that I get the Notice: Undefined index: txtamount0 and Notice: Undefined index: txtamount1 我的问题是我收到通知:未定义索引:txtamount0和注意:未定义索引:txtamount1

The name of your input is : txtamount.1 , not txtamount1 , so you should access it this way : 您输入的名称是: txtamount.1 ,而不是txtamount1 ,因此您应该以这种方式访问​​它:

for ($index = 0; $index < $number; $index++) {
    if (array_key_exists('txtamount.'.$index, $_POST))  {
        [...]

Or change your html : 或者更改你的html:

<td>|amount: <input type='text' name='txtamount$index'/></td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM