0

I want to open a pop up window with a form that contains inputs when I click a button. I don't have problem to do that, the particularity is I want as much inputs (in the form of the pop up) as cells of a row from my orginal window.

Basically I have page1.php that output SQL SELECT queries. For each line I generate a button that opens a pop-up window on onClick() event. I want to use this button to give users possibility to modify a line. img

I generate my table this way :

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

I don't see how to generate as much inputsas cells in a row

and also

how to set inputs default value with what is already filled with.

I want to learn from this, so, if possible, explain to me what I'm doing wrong here, or if my approach is lacking insight.

LyessD
  • 201
  • 1
  • 3
  • 13
  • 1
    The number of columns per row should probably be constant, yes? Then all you need to do is count how many elements $row contains, one time, to get the number. – misorude Jul 31 '19 at 08:16
  • @misorude Yes the number of columns is constant, but how can I from the popup window get cells content ? – LyessD Aug 20 '19 at 12:36
  • Isn’t that a _completely_ different question now? Normally you would just pass a row ID via your link/button, and then inside the script that provides the popup content, you make a database query to fetch the data of that particular row … – misorude Aug 20 '19 at 12:42
  • @misorude Well my question was about this 2 things. But I wasn't sure to make a database query again was necessary. As I'm learning by doing, I get lost on the method to use every time – LyessD Aug 20 '19 at 12:52

1 Answers1

1

In file page1.php you will do something like this:

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

Then, in file page2.php you will do something like this:

    $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;
IVO GELOV
  • 13,496
  • 1
  • 17
  • 26
  • It is not really what I'm looking for. Here you suggest me to add input on each cells. But I'm looking for a way to open a new window where I will have as much text input as cells. See on the image above, on button click, I want to open a pop up window where every cells content will be output in text input. I'm struggling to do this step – LyessD Aug 20 '19 at 12:35
  • I can not understand your issue. First you say you do not want text INPUTs inside the table - then you say you want as many INPUTs as there are cells (and in the question you say the INPUTs should have a pre-populated value). Since you are building the table - you obviously know (or can compute) the number of rows and columns. Once you know this number - you can use a simple FOR cycle to create the corresponding number of INPUTs and set their VALUE attribute to whatever you like/need. – IVO GELOV Aug 20 '19 at 13:02
  • There is 2 differents pages. On page1.php I output my table and values. I don't have any problems with that. On every line I have a button that, that open a new windows : page2.php I want on page2.php to populate as much text INPUT as I have fields on a row from page1.php. And that the text INPUTvalues are the values of the fields in the clicked line on page1.php – LyessD Aug 20 '19 at 13:10
  • Well, you simply ensure each and every row in the result from your query has an unique ID - and then provide this ID when you are fetching `page2.php` so that it can fetch the right record from DB. – IVO GELOV Aug 20 '19 at 14:58