简体   繁体   English

使用PHP和HTML在每个表行上生成的按钮

[英]Generated button on each table row with PHP and HTML

I have this code to generate buttons on each table row with PHP and HTML: 我有以下代码使用PHP和HTML在每个表行上生成按钮:

<!DOCTYPE HTML>
  <h2 class="text-center">Consulta</h2>
  <br>
  <table class="table table-striped">
    <tr class="info">
      <td class="black"><span style="font-weight:bold">#</span></td>
      <td class="black"><span style="font-weight:bold">Data</span></td>
      <td class="black"><span style="font-weight:bold">Condomínio</span></td>
      <td class="black"><span style="font-weight:bold">Entrada</span></td>
      <td class="black"><span style="font-weight:bold">Título</span></td>
      <td class="black"><span style="font-weight:bold">Autor</span></td>
      <td class="black"><span style="font-weight:bold">Estado</span></td>
      <td class="black"><span style="font-weight:bold"></span></td>
    </tr>
    <?php
    $select = "SELECT * FROM database";
    $get = mysqli_query($cnn, $select) or die ('error');
    while($row = mysqli_fetch_array($get, MYSQLI_ASSOC)) {
      echo "<tr>";
      echo "<td>";
      echo $row['id'];
      echo "</td><td>";
      echo $row['data'];
      echo "</td><td>";
      echo $row['condominio'];
      echo "</td><td>";
      echo $row['entrada'];
      echo "</td><td>";
      echo $row['titulo'];
      echo "</td><td>";
      echo $row['autor'];
      echo "</td><td>";            
      echo $row['estado'];
      echo "</td><td>";    
      $html = '<form role="form" class="form-inline" method="POST" enctype="multipart/form-data" action="thispage.php">';
      echo $html;
      echo '<button type="submit" name="alter'.$row['id'].'" id="alter'.$row['id'].'" class="btn btn-default btn-xl">alter'.$row['id'].'</a></button>';
      echo '</form>';

      echo "</td>";
      echo "</tr>";
    }
    echo "</table>";
?>
</html>

And, of course, I want to use the buttons, but the code below won't work for me: 而且,当然,我想使用按钮,但是下面的代码对我不起作用:

<?php
$var = "row['id']";
$var2 = "alter".$var;
if (isset($_POST['$var2'])) {
  echo('some code');
} 
?>

I saw some solutions with Javascript but I can only use PHP and HTML, so if anyone could help I would appreciate it. 我看到了一些使用Javascript的解决方案,但我只能使用PHP和HTML,因此,如果有人可以帮助,我将不胜感激。

in simple way replace button to link 以简单的方式替换按钮链接

when you get the data put a link like that 当您获取数据时,放置一个类似的链接

echo " <a href='?id=".$row['id']."'>'alter'".$row['id']."</a>"

in next page use it like that 在下一页像这样使用它

if(isset($_GET['id']))
{
do something 
}
if (isset($_POST[$var2])) {
  echo('some code');
} 

Try this. 尝试这个。 I guess $var2 is treated as a string since it is covered with single quote 我猜$var2被当作​​一个字符串,因为它$var2引号覆盖

If $var2 should read "alter1", "alter2", "alter3" etc. the line, 如果$ var2应该将“ alter1”,“ alter2”,“ alter3”等读为该行,

$var = "row['id']";

should read, 应该读,

$var = $row['id'];


Craig 克雷格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM