如何从单选按钮中获取多个值,然后将它们分配到单个变量中并在页面上显示


How can I get multiple values from a radio button and then assign them into single variables and display them on a page?

//一个主要问题是,一旦用户选择一个单选按钮并点击提交值,就会发送一个数组。我想把这些单独的查询(route_no、to_city、from_city、price)放在nextpage.php页面上的一个表中,放在它们的表头或标签旁边。我不能这样做,因为confirm值将所有查询作为单个字符串/数组发送。我该如何解决这个问题?请帮助我。在这里输入图像描述

这是我的html页面

<html>
<head>
<script>
function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        };
        xmlhttp.open("GET","display3.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>
<form>
<select name="to_city" onchange="showUser(this.value)">
  <option value="">Select a person:</option>
  <option value="Sydney">Sydney</option>
  <option value="Brisbane">Brisbane</option>
  <option value="3">Joseph Swanson</option>
  <option value="4">Glenn Quagmire</option>
  </select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>
</body>
</html>

这是我的display3.php页面

<!DOCTYPE html>
<style>
td{
    padding-top: 10px;
    padding-right: 10px;
    padding-bottom: 10px;
    padding-left: 10px;
}
</style>
<body>
<?php
$q = strval($_GET['q']);
$con = mysqli_connect('localhost','root','','mydb');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"flights");
$sql="SELECT * FROM flights WHERE to_city = '".$q."'";
$result = mysqli_query($con,$sql);
?>
<form action="nextpage.php" method="get">

   <?php
echo "<table>
<tr>
<th>Route_no</th>
<th>to_city</th>
<th>from_city</th>
<th>price</th>
<th>Confirm</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    $all_values = $row['route_no'] .",".$row['to_city'].",".$row['from_city'].",".$row['price'];
    echo "<tr>";
    echo "<td>" . $row['route_no'] . "</td>";
    echo "<td>" . $row['to_city'] . "</td>";
    echo "<td>" . $row['from_city'] . "</td>";
    echo "<td>" . $row['price'] . "</td>";
echo "<td><input type='radio' name='Confirm' value='".$all_values."'></td>";
    echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?> 
    <input type="submit" value="Submit">
</form>
</body>
</html>

这是我的下一页.php

<!DOCTYPE html>
<head>
<style>
td{
    padding-top: 10px;
    padding-right: 10px;
    padding-bottom: 10px;
    padding-left: 10px;
}
    </style>
    <body>
    <h1>Booking Details</h1>
    <h2>Flight No.</h2>
    <h2>to_city</h2>
    <h2>from_city</h2>
    <h2>Price</h2>

    <?php

    echo $_GET['Confirm']; 

    ?>
    <table >
    </body>
    </head>
    </html>

使用爆炸函数形成一个由,分隔的数组。

     <?php
       $str = "4,sydney,canberra,120.00";
        print_r (explode(",",$str)); 
      ?>
   out put 
         Array
       (
          [0] => 4
          [1] => sydney
          [2] => canberra
          [3] => 120.00
         )

是的,爆炸可以这样工作,在nextpage.php 中

<!DOCTYPE html>
<head>
<style>
td{    
    padding-top: 10px;
    padding-right: 10px;
    padding-bottom: 10px;
    padding-left: 10px;
}
</style>
<?php
  $details = explode(",", $_GET['Confirm']);
?>
</head>
<body>
<h1>Booking Details</h1>
<table>
  <tr>
   <th>Flight No.</th>
   <td><?=$details[0]?></td>
  </tr>
  <tr>
   <th>to_city</th>
   <td><?=$details[1]?></td>
  </tr>
  <tr>
   <th>from_city</th>
   <td><?=$details[2]?></td>
  </tr>
  <tr>
  <tr>
   <th>Price</th>
   <td><?=$details[3]?></td>
  </tr>    
</table>
</body>
</html>