Php单选按钮选择

时间:2015-12-13 04:44:36

标签: php

我无法让单选按钮与我的选择语句一起使用问题出现在我认为的按钮中,但我不确定这一点,所有帮助都表示赞赏

这是我目前的代码。我需要做的是页面刷新并将表格排序到按钮

选择的内容
    <?php require_once('dbadmin.php');?>
    <?php 
      session_start();
      $user = $_SESSION['user']; 
      if(!isset($user)){
        header("Location:admin_login.php");
      }
    ?>

    <h1>Reports</h1>
    <table border="1" style="width:100%">
  <tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Email</th>
    <th>Distance</th>
  </tr>
   <?php
    include('dbadmin.php');
    $select = "SELECT `fname`, `lname`, `email`, `distance` FROM runner ORDER BY `lname`";
    $result = mysql_query($select) or die ('Oops! '.mysql_error($connect));

    if(isset($_POST['submit_sort'])){
        $radio = $_POST['radiosort']; 
        if($radio =='fname'){
          $select = "SELECT `fname`, `lname`, `email`, `distance` FROM runner ORDER BY `fname`";}
        else if($radio =='lname'){
        $select = "SELECT `fname`, `lname`, `email`, `distance` FROM runner ORDER BY `lname`";}
        else if($radio =='distance'){
          $select = "SELECT `fname`, `lname`, `email`, `distance` FROM runner ORDER BY `distance`";}
          $result = mysql_query($select) or die("Invalid query: ".mysql_error($connect));
        }
        if (mysql_num_rows($result) > 0) {  
        while($row = mysql_fetch_assoc($result)) {
        echo '<tr><td>'.$row['fname'].'</td>';
        echo '<td>'.$row['lname'].'</td>';
        echo '<td>'.$row['email'].'</td>';
        echo '<td>'.$row['distance'].'</td></tr>';
      }
      }
    ?>
    <form name="reports" id="reports" action="reports.php">
          <label for="submit_sort" style="font-weight:bold;">Sort By:</label><br>
          <input name="radiosort" type="radio" title="sort" id="lname" value="lname">
          <label for="lname">Last Name</label><br>
          <input name="radiosort" type="radio" title="sort" id="fname" value="fname">
          <label for="fname">First Name</label><br>
          <input name="radiosort" type="radio" title="sort" id="distance" value="distance">     
          <label for="distance">Distance</label><br><br>
          <input type="submit" name="submit_sort" title="submit_sort" id="submit" value="Submit">
      </form>
    </table>

2 个答案:

答案 0 :(得分:0)

$radio = $_POST['sort']; 

应该是

$radio = $_POST['submit_sort'];

答案 1 :(得分:0)

在您的代码中,您正在检查if(isset($_POST['submit_sort'])){,但是 你没有在<form>元素上指定方法属性为&#34; POST&#34;。

更改此行:

<form name="reports" id="reports" action="reports.php">

到此:

<form name="reports" id="reports" action="reports.php" method="POST">