0

我想在单击按钮时打开一个包含输入的表单的弹出窗口。我这样做没有问题,特殊性是我想要与原始窗口中的一行单元格一样多的输入(以弹出窗口的形式)。

基本上我有page1.php那个输出 SQLSELECT查询。onClick()对于每一行,我都会生成一个按钮,用于在事件中打开一个弹出窗口。我想使用这个按钮让用户可以修改一行。 图像

我以这种方式生成我的表:

        $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;

我看不到如何inputs连续生成尽可能多的单元格

并且

如何inputs使用已填充的内容设置默认值。

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

4

1 回答 1

1

在文件中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;
于 2019-07-31T09:07:06.857 回答