douyong1905 2012-07-08 13:51
浏览 19
已采纳

PHP - 数组多维提交

im new here. i have problem and in great hurry :( scenario : i want to make an dynamic input form. user input a number of columns and rows they want. then, it generate the table as the number inputted in previous form. after that, user input a random number to the table, and click submit button. system will record the data and process it. finally, system will show the average (just the example).

table generate succesfully. the problem is how to save/read the data inputted in input form.

this is input.php

<tr> 
<td width="30%" align="left" valign="top">Input the number of columns     :<td      height="37" align="left" valign="top"><input type="text" name="colums" value="" /></td> 
</tr> 
<tr> 
<td align="left" valign="top">Input the number of rows :</td> 
<td width="70%" align="left" valign="top"><input type="text" name="rows" value="" /></td> 
</tr> 
<tr> 
<td align="left" valign="top">&nbsp;</td> 
<td align="left" valign="top"><input type="submit" name="button" id="button" value="Submit" /></td> 
</tr> 
</table> 
</form> 

and the result php like this..

<?php 
$columns = $_POST['columns']; 
$rows = $_POST['rows']; 

echo "<form method='post' action='process.php'>"; 
echo "<table>"; 

//made the rows 
for ($i= 0; $i <= $rows-1; $i++){ 

//and the colums 
echo "<tr>"; 
for ($j = 0; $j <= $colums-1; $j++) { 

//here is the input form, and each of data inputed here that i want to save it. 
$sum = array('[$i][$j]'); 
echo "<td> </td> 
    <td><input size='5' type='text' name='data".$sum."' /></td> 
"; 

}
echo "</tr>";  
} 


echo "<tr><td></td><td><input type='submit' name='submit' values='Submit' /></td></tr>"; 
//im not sure here in value='$sum' 
echo "<tr><td></td><td><input type='hidden' name='banyak' value='$sum' /></td></tr>"; 
echo "</table>"; 
echo "</form>"; 

// here,which one should i $_GET[''] ? 

?>

and the process.php show the average from data. i made it totally wrong in process.php

  • 写回答

3条回答 默认 最新

  • dtest84004 2012-07-08 14:06
    关注

    Check this:

    <?php 
    $columns = 3; //test value
    $rows    = 2; //test value
    
    echo "<form method='post' action='process.php'>"; 
    echo "<table>"; 
    
    //made the columns 
    for ($row_idx= 0; $row_idx <=$rows-1; $row_idx++){ 
    
        //and the rows 
        echo "<tr>"; 
        for ($col_idx = 0; $col_idx <= $columns-1; $col_idx++) { 
    
        //here is the input form, and each of data inputed here that i want to save it. 
        $sum = array('[$row_idx][$col_idx]'); //??? What's for that?
        echo "<td></td> 
            <td><input size='5' type='text' name='data[{$row_idx}][{$col_idx}]' /></td> 
        "; 
    
        } 
    }
    
    echo "</tr>"; 
    echo "<tr><td></td><td><input type='submit' name='submit' values='Submit' /></td></tr>"; 
    //im not sure here in value='$sum' 
    echo "<tr><td></td><td><input type='hidden' name='banyak' value='$sum' /></td></tr>"; 
    echo "</table>"; 
    echo "</form>"; 
    
    // here,which one should i $_GET[''] ? 
    
    echo "<PRE>";
    print_r($_REUQEST['data']);
    echo "</PRE>";
    
    ?>
    
    1. You sould name your variables to be understandable.
    2. You should name your input fileld like an array and it will be parsed by php, so <input name='data[1][2]' value="foo"> will "arrive" to by like $data = array('1' => array('2' => "foo"));
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题