2
.
.
.
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?

1
  • The mysql functions are deprecated. You should consider using newer functions, as defined here Commented Jun 23, 2012 at 7:55

2 Answers 2

2

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

1 Comment

If you want to retract your answer, at least explain why. Otherwise your answer stays as long as it is accepted.
2

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

Comments

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.