0

I have this code to generate buttons on each table row with PHP and 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.

7
  • try this $row["id"] Commented Feb 24, 2016 at 11:25
  • I know that part is working, because the button name comes out right if I make it show on each button. Commented Feb 24, 2016 at 11:28
  • change name of button to for example button1 then change this if $_POST['$var2'] to $_POST['button1'] .....and your code missing many logical thing Commented Feb 24, 2016 at 11:36
  • @mohade if I do that of course one of the buttons will work depending on the number, but I want all of them to work individualy. Commented Feb 24, 2016 at 11:39
  • ok will put answer and lets talk in answer comment Commented Feb 24, 2016 at 11:42

3 Answers 3

1

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 
}
Sign up to request clarification or add additional context in comments.

4 Comments

I did that and result is the same. Here's how the table looks (I have the button name showing on each button): imgur.com/ysLeEWl pretend the names are "alter" and not "alterar"
ok im editing my answer ,if you want to use this id to do something with it
that worked! But I really need to use a button for it to look nice and pretty, how can I?
You can use CSS to modify <a> and make it like button or anything else
0
if (isset($_POST[$var2])) {
  echo('some code');
} 

Try this. I guess $var2 is treated as a string since it is covered with single quote

5 Comments

ah sorry I forgot to change that variable, but that's not the problem at all.
@Manuel Updated the answer, Can you try this and let me know?
@Manuel How you are getting $row['id']. Can you print the value and check that you are getting correctly?.
I already printed an the value is correct, each button name is "alterx" (x being the row number)
@Manuel I don't see any issues in the code except whatever mentioned above. The only thing you need to check is, cross verifying form button names against the printed value. Just inspect the button elements and see you are getting same button names while printing.
0

If $var2 should read "alter1", "alter2", "alter3" etc. the line,

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

should read,

$var = $row['id'];


Craig

1 Comment

doesn't work because row['id'] is supposed to be a string to add to more code ( which is $var2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.