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

时间:2016-05-07 07:25:10

标签: javascript php html

//一个主要问题是,一旦用户选择了radiobutton并点击提交值,它就会发送一个数组。我想将这些单独的查询(route_no,to_city,from_city,price)放在其表格标题旁边或标签旁边的表格中的nextpage.php页面上。我不能这样做因为,确认值将所有查询作为单个字符串/数组发送。我该如何解决这个问题,请帮助我。enter image description here

这是我的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>

这是我的nextpage.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>

2 个答案:

答案 0 :(得分:0)

使用explode函数形成一个以。,

分隔的数组
     <?php

       $str = "4,sydney,canberra,120.00";
        print_r (explode(",",$str)); 

      ?>

   out put 

         Array
       (
          [0] => 4
          [1] => sydney
          [2] => canberra
          [3] => 120.00
         )

答案 1 :(得分:0)

是的爆炸可以这样工作,在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>