dth54864 2015-11-27 22:56 采纳率: 0%
浏览 21
已采纳

PHP动态用户输入到php表中

I am trying to insert user input into a php table. For example if I define a 2x3 table (2 rows and three columns) from a previous page, an input screen displays with 6 inputs for you to fill in whatever you would like to. The 6 content variables are then inserted into a table. I believe an array and possibly a foreach loop may be necessary. However, I am not sure how to get the user input content to carry over to the next page and inserted into a table properly. I also trying to get it into a list, but I assume if the table problem can be solved, it should be the same for the lists. Any help would be greatly appreciated.

//Page where defined inputs are displayed

$row = (isset($_POST['rows']) ? $_POST['rows'] : null);
$col = (isset($_POST['columns']) ? $_POST['columns'] : null);
$list = (isset($_POST['listitems']) ? $_POST['listitems'] : null);
$list2 = (isset($_POST['listitems2']) ? $_POST['listitems2'] : null);

array('[content[]');//Not sure if array should be here or on next page

$y = $list;
$z = $list2;
$x = $row * $col;

        for ($i = 1; $i <= $x; $i++) {

            echo "$i<input type='text' name='content[]' required> <br>";

        }
        for ($t = 1; $t <= $y; $t++) {

            echo "$t<input type='text' name='content[]' required> <br>";
        }

        for ($s = 1; $s <= $z; $s++) {

            echo "$s<input type='text' name='content[]' required> <br>";
        }

         echo   "<input type='submit' name='submit2' value='Submit'/>
            <input type='hidden' name='method' value='post' />
            <input type='hidden' name='unorderedinput' value='$list' />
            <input type='hidden' name='orderedinput' value='$list2' />
            <input type='hidden' name='rowsinput' value='$row' />
            <input type='hidden' name='columnsinput' value='$col' />
            <input type='hidden' name='cont' value='content[]' />";

echo "</form>";

//page where the table is displayed.

        $trow = $_POST['rowsinput'];
        $tcol = $_POST['columnsinput'];
        $ulist = $_POST['unorderedinput'];
        $olist = $_POST['orderedinput'];
        $tcontent = $_POST['cont'];

        echo "<table border='1'>";

        for ($tr = 1; $tr <= $trow; $tr++) {
            echo "<tr>";
                for ($td = 1; $td <= $tcol; $td++) {
                    echo "<td align='center'>". "$tcontent". "</td>";//I'm not sure if this is correct
                }

                    echo "</tr>";
            }

        echo "</table>";
  • 写回答

2条回答 默认 最新

  • dongyan1841 2015-11-27 23:41
    关注

    First, you don't need this line:

        array('[content[]');
    

    It does not do anything, because it is not assigned to a variable. If the purpose was to make a content array, that is something php will do for you on the second page. See below.

    Also, you don't need this line either:

        <input type='hidden' name='cont' value='content[]' />";
    

    That is because you already have input tags with name content[], so use those directly in the next page, where you had this line:

        $tcontent = $_POST['cont'];
    

    That line just made $tcontent = "content[]", i.e. the literal string, not the content.

    Instead put:

        $tcontent = $_POST['content'];
    

    ... and now you will have an array of values.

    Then, also on the second page, you could iterate over these values using the current and next functions:

        echo "<table border='1'>";
    
        $value = current($tcontent);
        for ($tr = 1; $tr <= $trow; $tr++) {
            echo "<tr>";
            for ($td = 1; $td <= $tcol; $td++) {
                echo "<td align='center'>". $value . "</td>";
                // get next element
                $value = next($tcontent);
            }
            echo "</tr>";
        }
    
        echo "</table>";
    

    You should not forget to make your code deal with missing arguments in $_POST, using the isset function, like you did for the first page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?