如何获得与 html 中显示的行一样多的输入?

How to get as much inputs as line displayed in html?

我想打开一个弹出窗口 window,其中包含单击按钮时包含输入的表单。我这样做没有问题,特殊之处在于我想要与原始 window.

中的一行单元格一样多的输入(以弹出窗口的形式)

基本上我有 page1.php 输出 SQL SELECT 查询。对于每一行,我都会生成一个按钮,用于在 onClick() 事件上打开 pop-up window。 我想用这个按钮给用户修改一行的可能性。

我这样生成 table:

        $result = Db::query($requete);

        $texte = "<table class='table table-bordered table-sm'><thead>$table_header</thead>";
        $texte .= "<tbody>";

        if (!pg_num_rows($result)){
            $nb_ligne = "Aucune ligne trouvée !";
        }else{
            $nb_ligne ="Nombre de lignes : ".pg_num_rows($result);
        }
        while($row = pg_fetch_row($result)){
            $texte .= "<tr><td><button class=\"btn btn-primary\" type=\"button\" id=\"buttonCreate\" onclick=\"OuvrirPopup('update.php','', 'resizable=no, location=no, width=800, height=1000, menubar=no,status=no, scrollbars=no')\"></button></td>";
            foreach ($row as $value){
                $texte .= "<td style='word-break: keep-all'>$value</td>";
            }
            $texte .= "</tr>";
        }
        $texte .= "</tbody></table>";

        $response = new Response();
        $response->assign('nb_ligne', 'innerHTML', $nb_ligne);
        $response->assign('tableau_resultat', 'innerHTML', $texte);

        return $response;

我看不出如何连续生成 inputsas 个单元格

还有

如何将inputs默认值设置为已经填充的值。

我想从中吸取教训,所以,如果可能的话,请向我解释我在这里做错了什么,或者我的方法是否缺乏洞察力。

在文件 page1.php 中,您将执行如下操作:

    $result = Db::query($query); // the query should return column ID for each row

    $text = "<table class='table table-bordered table-sm'><thead>$table_header</thead><tbody>";

    if (!pg_num_rows($result))
    {
      $no_results = "Nothing was found !";
    }
    else
    {
      $no_results = "Results found: ".pg_num_rows($result);
    }
    while($row = pg_fetch_row($result))
    {
      // the popup will open `page2.php?id=ID-of-the-row`
      $text .= "<tr><td><button class='btn btn-primary' type='button' id='buttonCreate' onclick='showPopup(\"update.php?id=".$row['id']."\")'>EDIT</button></td>";
      foreach ($row as $value)
      {
        $text .= "<td style='word-break: keep-all;'>".htmlspecialchars($value)."</td>";
      }
      $text .= "</tr>";
    }
    $text .= "</tbody></table>";

    $response = new Response();
    $response->assign('no_results', 'innerHTML', $no_results);
    $response->assign('table_body', 'innerHTML', $text);

    return $response;

然后,在文件 page2.php 中,您将执行如下操作:

    $result = Db::query($query); // the query will use $_GET['id'] in order to fetch the specific row

    $text = "<table class='table table-bordered table-sm'><thead>$table_header</thead><tbody>";

    if (!pg_num_rows($result))
    {
      $no_results = "Nothing was found !";
    }
    else
    {
      $no_results = "Results found: ".pg_num_rows($result);
    }
    // there should be no more than 1 row
    while($row = pg_fetch_assoc($result))
    {
      $text .= "<tr>";
      foreach ($row as $key => $value)
      {
        $text .= "<td style='word-break: keep-all;'><input type=text name='".$key."' value='".htmlspecialchars($value)."'></td>";
      }
      $text .= "</tr>";
    }
    $text .= "</tbody></table>";

    $response = new Response();
    $response->assign('no_results', 'innerHTML', $no_results);
    $response->assign('table_body', 'innerHTML', $text);

    return $response;