1

I'm fairly new to php and I know the question has been asked a lot, but please bear with me. I've managed to populate an html table with a mySql database. At the end of each row is a check in button that resets the date on the row. My question is how, can I keep track of which row the button belongs too so I know to only update that row?

I have an ID on each row so I know I can use that when updating to the table, but I'm just uncertain how to associate each button to it's respective row. Here's a snippet:

while ($row = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td class='text-left'>".$row[card_id]</td>";
    echo "<td class='text-left'>".$row[status]</td>";
    //current student
    echo "<td class='text-left'>".$row[user_id]."</td>";
    echo "<td class='text-left'>".$row[end_date]."</td>";

    //Early Check In button
    echo "<td class='text-left'><button class='checkInBtn'></button></td>";
    echo "</tr>";
}
halfpastfour.am
  • 5,162
  • 2
  • 37
  • 58
Will Tuttle
  • 450
  • 1
  • 10
  • 31
  • your html is broken. you are outputting MULTIPLE `id=checkInBtn`, which is illegal. an `id` MUST be unique across the **ENTIRE** document. – Marc B Oct 11 '16 at 18:47
  • the php also has errors. if you can clean up your code it would get you better answers. – Funk Doc Oct 11 '16 at 18:49
  • 1
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Oct 11 '16 at 18:51
  • Which php errors do you mean? – Will Tuttle Oct 11 '16 at 19:01
  • Can you be more specific in your question that we can help you.. – Igbaryya Oct 11 '16 at 19:02

2 Answers2

1

One way to do it is to have a "click event" attached to the button with the ID of that row passed to the event, so any time the button is clicked, you know which row it was based on the ID.

azdonald
  • 186
  • 2
  • 13
1

You can send the id to a js function using click event. You can also create a form, inside the form you can create an input hidden and assign it the value, and you can use this value when user submit the buttom.

With this id you can modify the info, and create other view to modify it.

migueref
  • 302
  • 1
  • 7