PHP单选按钮获取两个值

时间:2014-04-02 21:35:51

标签: php mysql forms post radio-button

因此,下面的代码从用户选择的行中获取server_id。但是,我想知道是否有可能从行中获取server_id和server_name而用户不必再做他已经做过的事情(选择一个单选按钮并按下提交)。谢谢你的帮助。

<form method="post" name="server_information" id="server_information" action="test.php">
<label>Server Information</label><br><br>
<table border='1'>
    <tr>
        <th>Selection</th>
        <th>User Name</th>
        <th>Date Created</th>
        <th>Server Name</th>
        <th>Server Id</th>
        <th>Public DNS</th>
        <th>Server Status</th>
    </tr>
    <?php while($row = mysql_fetch_assoc($result)): ?>
    <tr>
        <td><input type="radio" name="server" value="<?php echo $row['serverId']; ?>" /></td>
        <td><?php echo $row['userName']; ?></td>
        <td><?php echo $row['dateCreated']; ?></td>
        <td><?php echo $row['serverName']; ?>"</td>
        <td><?php echo $row['serverId']; ?></td>
        <td><?php echo $row['publicDNS'] ?></td>
        <td><?php echo $row['serverStatus']; ?></td>
    </tr>
    <?php endwhile; ?>
</table>
<br>
<?php endif; ?>

<input type="submit" name="server_stop" value="Stop Server"/>
<input type="submit" name="server_terminate" value="Terminate Server"/>
</form>

3 个答案:

答案 0 :(得分:2)

您可以将管道|或任何其他字符连接到无线电值,然后在管道上爆炸$ _POST ['server'],您将在索引0中获取id并在索引中命名1。

<td><input type="radio" name="server" value="<?php echo $row['serverId'].'|'.$row['serverName']; ?>" /></td>

另一种解决方案是添加一个隐藏的输入,并将其命名为服务器ID,如下所示:

<td>
<input type="radio" name="server" value="<?php echo $row['serverId']; ?>" />
<input type="hidden" name="name_<?php echo $row['serverId'];?>" value="<?php echo $row['serverName'];?>" />
</td>

然后在PHP中

  $server_id=(int)$_POST['server'];
  $server_name=$_POST['name_'.$server_id];

答案 1 :(得分:0)

当然,只需将两个值用逗号(或其他不会出现在值中的分隔符)连接起来:

<tr>
    <td><input type="radio" name="server" 
               value="<?php echo $row['serverId'] . ',' . $row['serverName']; ?>" /></td>
    <td><?php echo $row['userName']; ?></td>
    <td><?php echo $row['dateCreated']; ?></td>
    <td><?php echo $row['serverName']; ?>"</td>
    <td><?php echo $row['serverId']; ?></td>
    <td><?php echo $row['publicDNS'] ?></td>
    <td><?php echo $row['serverStatus']; ?></td>
</tr>

然后,当您处理表单时,使用explode()分隔结果:

$server_info = explode(",", $_POST["server"]); // server_info is an array
$server_selection = $server_info[0];
$server_name      = $server_info[1];

答案 2 :(得分:0)

您可以使用数组:

name="server[$row['serverId']]" value="$row['serverName']"

然后得到它:

list($id, $name) = $_POST['server'];