h3tr1ck
h3tr1ck

Reputation: 909

PHP Radio button grab two values

So the below code grabs the server_id from the row that the user selects. However, I am wondering if it is possible to grab both the server_id and server_name from the row without the user having to do anymore than he already does (select a radio button and push submit). Thanks for any help.

<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>

Upvotes: 1

Views: 1266

Answers (3)

Peter Gluck
Peter Gluck

Reputation: 8236

Sure, just concatenate the two values with a comma (or other delimiter that won't appear in the values):

<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>

Then when you process the form use explode() to separate the results:

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

Upvotes: 0

AbraCadaver
AbraCadaver

Reputation: 78994

You can use an array:

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

Then get it with:

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

Upvotes: 0

CodeBird
CodeBird

Reputation: 3858

You can put a pipe | or any other character to concatenate both in the radio value, then explode your $_POST['server'] on pipe, you'll get id in index 0 and name in index 1.

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

Another solution would be to add a hidden input and name it with the server id like this:

<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>

then in PHP

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

Upvotes: 2

Related Questions