如果在php中选择了一个单元格上的单选按钮,如何获取整行值


How to get whole row value if radio button on one cell is selected in php

我有一个表,其中的数据来自数据库。前2列的数据来自另一个数据库查询,其他列来自另一查询。该表位于以下链接中:表pic

现在我想做的是,每当一个单元格中的任何单选按钮被选中时,我就会获得php中同一行的前两列的值,然后将其发送到另一个页面。代码为:

<?php
      $trainName;
      $trainNo="";
      $depart_time="";
      $train_name="";
      $class_id="";
      $full_berth="";
      $full_seat="";
      $seat="";
      $berth="";
      $con = $a->operation();
      if(($st_No2-$st_No)<1){
          $sql = "select t.train_no, t.train_name, TIME_FORMAT(tt.depart_time, '%H:%i:%s') As depart_time from trains t, train_timings tt where t.train_No = tt.train_No and tt.st_Code = '" .$_SESSION['from']. "' and t.train_No like '%DN'";
          $r = mysql_query($sql);

          echo "<tr >
      <col width='100' >
      <col width='80' >";
      while($row=mysql_fetch_array($r)){
      $trainNo = $row["train_no"];
      $trainName = $row["train_name"];
        $sql4="select seat, berth, class_id from seat where train_no='$trainNo' and date='" .$_SESSION['dep_date']. "'"; 
         $r4= mysql_query($sql4);
         while($row4=mysql_fetch_array($r4)){
             $seat=$row4['seat'];
         $berth=$row4['berth'];}
           if($seat>0 and $berth>0){
             echo "<td>".$row["train_name"]."</td>"."<td>".$row["depart_time"]."</td>";
         //$trainNo = $row["train_no"];
         $depart_time= $row["depart_time"];
         $train_name= $row["train_name"];
            $query = "select c.class_id, f.full_berth, f.full_seat from class c inner join fare f on c.class_id = f.class_id where t_from='" .$_SESSION['from']. "' and t_to='" .$_SESSION['to']. "' and f.train_no='$trainNo'";
         $r1 = mysql_query($query);
         while($row=mysql_fetch_array($r1)){
            $class_id= $row["class_id"];
            $full_berth= $row["full_berth"];
            $full_seat= $row["full_seat"];
            echo $row["class_id"];
            //echo "<td>".$class_id.$full_berth."</td>"."<td>".$class_id.$full_seat."</td>";
            echo "<td><input type='hidden' id='class1' value='$trainNo'><input type='hidden' id='class1' value='$class_id'><input type='radio' name='f1' value='$full_berth' onclick='classid()'><p>".$row["full_berth"]."&nbspPKR</p></td>"."<td><input type='hidden' id='class2' value=".$row["class_id"]."><input type='hidden' id='class2' value='$class_id'><input type='radio' name='f1' value='$full_seat' onclick='classid2()'><p>".$row["full_seat"]."&nbspPKR</p></td>";
            $class_id = "";
            //if(isset('f1')){
                //echo "$row['class_id']";
            //}

            //echo "<td><input type='radio' name='f1' value='$row['full_berth']'".$row["class_id"].$row["full_berth"]."</td>"."<td>".$row["class_id"].$row["full_seat"]."</td>";
            }  
           }
           echo "</tr>";
           }

PHP是一种服务器端语言,这意味着它只与服务器交互,而不与浏览器交互。客户端语言(如JavaScript)可以对客户端事件(如单击)做出反应。

因此,要做到这一点,您需要JavaScript。我将使用jQuery来帮助简化我的示例。

如果您有一个带有单选按钮的多行简单表,那么您可以简单地创建JavaScript事件来将数据发送到PHP脚本。

$("input:radio").change(function() {
  if($(this).is(":checked")) {
    var col1 = $(this).parent().parent().children("td:first");
    var col2 = col1.next();
    alert(col1.text() + " " + col2.text());
    /*  // send to php using AJAX
    $.ajax({
      url: "#",
      data: {col1: col1, col2: col2},
      method: "post"
    });
    */
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr><td>Row1Col1</td><td>Row1Col2</td><td><input type="radio"></td></tr>
  <tr><td>Row2Col1</td><td>Row2Col2</td><td><input type="radio"></td></tr>
</table>