1

I have create an HTML table that displays information from a MySQL table with PHP. Here is my current code:

<table>
<tr>
    <th>Name</th>
    <th>Email</th>
    <th>Program</th>
    <th>Class</th>
</tr>

<?php

$db = JFactory::getDBO();

$query = "SELECT * FROM  student_management_module";

$db->setQuery($query);
$rows = $db->loadObjectList();

foreach ($rows as &$row) {
    echo "<tr>";
    echo "<td>" . "<input type=text name=fullName id=name_val value=" .$row->name. "> </td>";
    echo "<td>" . "<input type=text name=email id=email_val value=" .$row->email. "> </td>";
    echo "<td>" . "<input type=text name=prog id=prog_val value=" .$row->program. "> </td>";
    echo "<td>" . "<input type=text name=class id=class_val value=" .$row->class. "> </td>";
    echo "<td>" . "<input type=hidden name=hidden value=" .$row->student_id. "> </td>";
    echo "</tr>";
}

?> </table>

I want to add an update function that sends a row of edited information from the html table to the database. I've tried using HTML forms but they only be used to send the entire table of information or a singular cell of information.

Any help would be much appreciated!

Kilter
  • 41
  • 5
  • You will either need to provide a separate `
    ` that will submit only this row to the server or use JavaScript to get the values from this row and send them to the server using AJAX.
    – Mike Jul 12 '18 at 03:27
  • 1
    On another note, you probably don't want to be creating a reference in your `foreach` loop, and doing so can exhibit [unexpected behavior](https://stackoverflow.com/questions/4969243/strange-behavior-of-foreach) if you're not aware of it. – Mike Jul 12 '18 at 03:30
  • what would be the ideal way to update the rows in my table? – Kilter Jul 12 '18 at 22:11
  • Either way should work. AJAX is usually a bit more convenient to the user, but requires javascript. Using plain `
    ` tags will be universally supported. It depends on how you want your UX to be.
    – Mike Jul 12 '18 at 22:19
  • so if I wanted to use form tags, can I wrap it around an entire row? Wouldn't that be invalid HTML? – Kilter Jul 12 '18 at 23:47
  • Yes, it would be invalid HTML. You would have to modify the HTML to make it valid. For example, using `
    ` tags and setting them to behave like tables, or having a separate table for each row.
    – Mike Jul 13 '18 at 03:47

0 Answers0