添加单选按钮到MySQL获取结果并使用结果值来获取另一条记录......?

时间:2017-05-06 19:13:05

标签: php mysql radio-button radiobuttonlist

我希望默认选中第一个单选按钮。当我点击获取时间表时,我想使用WHERE子句获取该无线电选择的列车编号的时间表。

<?php

while ($res = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td><input type = radio />" . $res['Train_no'] . "</td>";

    ?>

    <form action="schedule.php" method="POST" class="form-inline">
        <input type="submit" value="Get Schedule"/>
    </form>
<?php

}


<?php

$con = mysql_connect("localhost", "root", "");

if ($con) {
    $db = mysql_select_db('traindb', $con);
} else {
    die('Could not connect: ' . mysql_error());
}


    $selected_val = $_POST['Train_no'];  // Storing Selected Value In Variable
    echo "You have selected :" . $selected_val;  // Displaying Selected Value


$result = mysql_query("SELECT * FROM train_detail WHERE Train_No='$selected_val'");
mysql_close($con);
?>

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

if(isset($_POST['Submit'])){
   $selected_val = $_POST['Train_no'];  // Storing Selected Value In 
   echo "You have selected :" .$selected_val;  // Displaying Selected Value

   $result = mysql_query("SELECT * FROM train_detail WHERE 
   Train_No='$selected_val'");
}

答案 1 :(得分:0)

以下是“选择”的一个示例:

<form action="" method="post">
<select type="age" class="form-control" id="age" name="age">
     <!-- <option value="disable" selected="">Please Select</option> -->
     <option value="">Please select</option>
     <option value="Under 35">Under 35</option>
     <option value=">35 - 44">35 - 44</option>
     <option value=">45 - 54">45 - 54</option>
     <option value=">55 - 59">55 - 59</option>
     <option value=">60 - 64">60 - 64</option>
     <option value=">65 - 69">65 - 69</option>
     <option value=">70 - 74">70 - 74</option>
     <option value=">75 - 79">75 - 79</option>
     <option value="80 +">80 +</option>
</select>
<input type="submit" value="Submit">
</form>

<?php
$age = $_POST['age'];
if (isset($_POST['age']) &&  $_POST['age'] == "")
echo "You did not choose any options. Pls try again.";
else {
echo $age;
}
?>

如果你想在选择选项中嵌入PHP,可以这样做:

<option value="<?php echo $res['Train_no'] ?>"><?php echo $res['some_other'] ?></option>

对于单选按钮,它是这样的:

<!DOCTYPE html>
<html>
<body>

<form action="/action_page.php">
  <input type="radio" name="gender" value="<?php echo $res['Train_no'] ?>" checked><?php echo $res['some_other'] ?><br>
  <input type="radio" name="gender" value="female"> Female<br>
  <input type="radio" name="gender" value="other"> Other<br><br>
  <input type="submit">
</form> 

</body>
</html>