简体   繁体   中英

How to POST HTML table data created from PHP

.
.
.
while ( $row = mysql_fetch_array($res) )
{                   
    echo "<tr>";
    echo "<td><input type=\"radio\" name=\"mod\" /></td>";
    echo "<td>" . $row['a'] . "</td>";
    echo "<td>" . $row['b'] . "</td>";
    echo "<td>" . $row['c'] . "</td>";
    echo "<td>" . $row['d'] . "</td>";
    echo "<td>" . $row['e'] . "</td>";
    echo "</tr>";
}
echo "</table>

Printing database data to an HTML table with PHP "echo".

For example, when this displays, there will be X rows of 6 columns (5 data and one a radio button).

For the row selected with the radio button, I need all of that specific rows data to POST to another .php page.

My initial thought was create an onClick() function for the radio button, with the data as parameters to the functions, then, having the function do an AJAX POST with that data.

or should I create a form around the table? If so, how would I get that rows data as form data?

Is there a better way?

The best way to do this would probably be to have the radio button value correspond to the row number. Once the form is submitted, retrieve that row from the database.

.
.
.
echo "<tr>";
echo "<td><input type=\"radio\" name=\"mod\" value=\""./*row number*/."\" /></td>";
echo "<td>" . $row['a'] . "</td>";
echo "<td>" . $row['b'] . "</td>";
echo "<td>" . $row['c'] . "</td>";
echo "<td>" . $row['d'] . "</td>";
echo "<td>" . $row['e'] . "</td>";
echo "</tr>";
.
.
.

And in the form handler

$rownumber = $_POST["mod"]
// whichever mysql function - make the same query as the first page
// get row $rownumber

If you have some ID for each row you should simply associate it with the radio button and once submitted your PHP script will be able to determine which was selected based on the sent id.

echo "<td><input type=\"radio\" name=\"mod\" value=\"" . $row['id'] . "\" /></td>";

Also generally I would suggest not to print HTML from PHP but embed it as it is supposed to be done

<?php while ( $row = mysql_fetch_array($res) ) : ?>
<tr>
<td><input type="radio" name="mod" value="<?php echo $row['id'] ?>" /></td>
<td><?php echo $row['a'] ?></td>
<td><?php echo $row['b'] ?></td>
<td><?php echo $row['c'] ?></td>
<td><?php echo $row['d'] ?></td>
<td><?php echo $row['e'] ?></td>
</tr>
<?php endwhile; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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