如何:HTML表格行单击将导致POST表单链接到后续页面


How: HTML table row click results in POST form linking to subsequent page

晚上好,我有一个表单,它是从sql数据库中填充的。

    <table class="sortable"  border="2" cellspacing="2" cellpadding="2">
    <tr>
        <th> Serial Number </th>
        <th> Date </th>
        <th> Time </th>
        <th> Color </th>
        <th> Design </th>
        <th> Result </th>
    </tr>
<?php
    $i = 0;
    while ($i < $num)
    {
        $serial = mysql_result($results, $i, 'serial_number');
        $date = mysql_result($results, $i, 'date');
        $time = mysql_result($results, $i, 'time');
        $airport = mysql_result($results, $i, 'color');
        $terminal = mysql_result($results, $i, 'design');
        $result = mysql_result($results, $i, 'result');
?>
    <tr>
        <td> <?php echo $serial; ?> </a></td>
        <td> <?php echo $date; ?> </td>
        <td> <?php echo $time; ?> </td>
        <td> <?php echo $color; ?> </td>
        <td> <?php echo $design; ?> </td>
        <td> <?php echo $result; ?> </td>
    </tr>
<?php
    $i++;
    }
?>

我想做的是让表格的每一行都可以点击。单击每一行时,该行中的一个单元格(第一个单元格)将通过(POST)发送到下一页。表格可以集成到每个tr中吗??

您在标记中指定了jQuery,所以我认为您可以使用它。

// when any row is clicked
$('table').on('click', 'tr', function () {
  // get the value
  var value = $(this).find('td:first').text();
  // redirect the user with the value as a GET variable
  window.location = nextPage + '?data=' + value;
});

其中nextPage是要重定向到的页面的URL。

简短的回答是:是的,一个表单可以集成到每个tr.中

答案很长-像以前一样使用:

<tr>
    <td> 
      <form method="post" target="details.php">
          <input type="submit" name="more" value="<?php echo $serial; ?>"
      </form>
      <?php echo $serial; ?>
    </td>
    <td> <?php echo $date; ?> </td>
    <td> <?php echo $time; ?> </td>
    <td> <?php echo $color; ?> </td>
    <td> <?php echo $design; ?> </td>
    <td> <?php echo $result; ?> </td>
</tr>

但是GET更容易,制作一系列链接到details.php?number=123,或两者中的任意一个:

    <td> 
      <a href="details.php?number=<?php echo $serial; ?>"
      <?php echo $serial; ?>
      </a>
    </td>

虽然get可以使用表单,但发送的数据不是用户自定义的,因此表单数据可以像链接一样生成使用。

在创建表时尝试在echo中使用该代码:

<td><?php echo('<a href="my_page.php?action&serial='.$serial.'">'.$serial.'</a>');?></td>

对于您拥有的每一个数据!

无作用的其他解决方案

<td><?php echo('<a href="my_page.php?serial='.$serial.'">'.$serial.'</a>');?></td>

my_page.php-将数据发送到何处

?[variable_name]-数据存储在何处