h3tr1ck
h3tr1ck

Reputation: 909

Radio Button POST issue

So here is the issue. I am running a mysql query and throwing the results of that query into radio buttons with a while statement. The amount of radio buttons can vary, so I can't do a set number of them with the POST function. I'm struggling with making it so that the user can select one of the rows and then the "serverId" data from the query is passed on to the test.php page. Any help would be greatly appreciated. Thanks.

<html>
 <head>
  <title>Server Information</title>
 </head>
 <body>

<?php

//Starts the session and grabs the username from it
session_start();
$name=mysql_real_escape_string($_SESSION['username']);

include 'header.php';
include 'mysql_connect.php';

mysql_select_db('ist421test');

$result = mysql_query("SELECT userName, dateCreated, serverName, serverId, publicDNS, isRunning FROM server_tbl WHERE userName='$name' ORDER BY dateCreated DESC") or die(mysql_error());

if(mysql_num_rows($result) > 0): ?>
<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>Running</th>
</tr>
<?php while($row = mysql_fetch_assoc($result)): ?>
<tr>
    <td><input method="post" type="radio" name="server" value="server_information" action="test.php"></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['isRunning']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<input type="submit" name="server_stop" value="Stop Server"/>
<input type="submit" name="server_terminate" value="Terminate Server"/>
</form>
<?php endif; ?>

</body>
</html>  

Upvotes: 1

Views: 90

Answers (1)

Wireblue
Wireblue

Reputation: 1509

Ok, a couple of recommendations.

First, for the radio input delete "server_information" from the value tag and paste in your serverID echo. For example.

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

Notice the method and action attributes have been removed. They're only applicable for form elements.

Give that a go now. Only one radio button should be selectable, and only one Server ID should be POST'ed to your form.

You can access the POST'ed variable on test.php using $_POST['server'].

Upvotes: 1

Related Questions