如何在一行中创建提交表单并将输入关联到该行


How to create submit form in a row and relate input to that row?

嘿,伙计们,我有一个问题要问你们。

想象一下,我希望能够记录我每周跑了多少英里,这样我就可以将它与我每周设定的目标进行比较。所以我用mysql_fetch_row创建了这个表。

$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
    <tr class='toprow'>
    <th>Week</th>
    <th>Goal</th>
    <th>Actual Number of Miles</th>
    </tr>";
while($row = mysql_fetch_row($result))
{
    echo "<tr class='standardrow'>";
    echo "<td>$row[0]</td>";
    echo "<td>$row[1]</td>";
    echo "<td><form><input method='post' type='number'></form></td>";
    echo "</tr>";
}
echo "</table>";

这段代码生成了一个包含10周和10个目标的表,以及一个用于表示实际里程数的列。这一列应该包括10个输入表单,其中可以提交实际的里程数。但是,如何将提交表单的输入与提交表单所在的行关联起来呢?主要的关键字是星期,所以这是与之相关的。

希望你能理解我的问题是什么

要做到这一点,您可以使用隐藏的输入字段。

当您echo每一行时,以及该行中的表单,您只需添加额外的行:

`<input type="hidden" name="row_id" value="' . $row['id_column'] . '" />';

完整的代码应该是:

$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
    <tr class='toprow'>
    <th>Week</th>
    <th>Goal</th>
    <th>Actual Number of Miles</th>
    </tr>";
while($row = mysql_fetch_row($result))
{
    echo "<tr class='standardrow'>";
    echo "<td>$row[0]</td>";
    echo "<td>$row[1]</td>";
    echo "<td>
            <form>
                <input method='post' type='number'>
                <input type='hidden' name='row_id' value='" . $row['id_column'] . "' />
            </form>
          </td>";
    echo "</tr>";
}
echo "</table>";

我认为应该有一些修改必须在循环中完成。

echo "<td><form  method='post'><input type='number' value='".$rows['col_name']."'><input type='submit' ></form></td>";

此代码向每行添加一个提交按钮。但是,这不应该是我想的那样。应该是这样,

echo "<form  method='post'> ";
while($row = mysql_fetch_row($result))
{
    echo "<tr class='standardrow'>";
    echo "<td>$row[0]</td>";
    echo "<td>$row[1]</td>";
    echo "<td><input type='number' value='".$rows['col_name']."'></td>";
    echo "</tr>";
}
echo "<input type='submit' ></form>";

或者让输入字段看起来像这样

'<input type="text" name="row['.$row['id_column'].'][miles]" />';

当你发送它时,它将返回一个数组。

foreach($_POST['row'] as $key => $value){
// $key is your primary key
// $value['miles'] is your input value
}