【问题标题】:User filled HTML table to php array用户将 HTML 表填充到 php 数组
【发布时间】:2014-08-11 19:56:56
【问题描述】:

我已经从一个数组创建了一个 HTML 表格,但是一些单元格是输入。这个想法是在单元格和一些输入框中输入您自己的数据。

我将 html 表单保存到我在数组中使用的变量中。看起来像这样:

$input='<form action="test.php" method="post"><input type=text></input></form>';

 $array = array(
    array('1', '2', $input, '4', '5', '6', $input, '8', $input, '10'),
    array('11', $input, '13, $input, '15', '16', $input, '18', '19', $input),);

test.php 是比较代码所在的位置。

填满整个表格后,用户按下回车键,它会通过将数字与包含所有数字的类似数组进行比较来告诉他们数字的顺序是否正确。

我尝试将$_POST 与包含答案的数组进行比较,但没有那么简单。

我只想知道如何将整个表格(具有固定数据和输入数字)保存到一个像上面那样的数组中,以便我能够比较它们。

【问题讨论】:

    标签: php html arrays html-table


    【解决方案1】:

    对于 php 中的 $_POST 或 $_GET 数组,您应该在 name 属性中使用数组形式: How to POST an associative array in PHP

    <input type="text" name="num[]">
    

    为您的代码测试:

    主页: $input='';

     $array =array('1', '2', $input, '4', '5', '6', $input, '8', $input, '10');
    
    echo '<table><tr>';
    echo '<form action="test.php" method="post">';
    foreach($array as $num){
        if($num != $input){
            echo '<td><input type="hidden" value="'.$num.'" name="num[]">'.$num.'</td>';
        } 
        if($num == $input){
            echo '<td><input type="text" name="num[]"></td>';
        }
    
    }
    echo '<input type="submit"></form>';
    
    echo '</table></tr>';
    

    还有测试页面:

    $num=$_POST['num'];
    var_dump($num);
    

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 2012-11-20
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多