Вопрос

Below is a piece of the code that input the results of a query into radio buttons. Then, when the user pushes one of the buttons in the form, it moves to the pages test.php. I have run into a problem though that the second code (which is on test.php) doesn't seem to work although I can't see any reason that it shouldn't. Any ideas are awesome. Thanks.

<?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['isRunning']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<br>
<?php endif; ?>

<form method="post" name="server_information" id="server_information" action="test.php">
<input type="submit" name="server_stop" value="Stop Server"/>
<input type="submit" name="server_terminate" value="Terminate Server"/>
</form>

</body>
</html>  

Second Code:

<?php
if (isset($_POST['server_stop'])) {
    echo "Server Stopped";
}

if (isset($_POST['server_terminate'])) {
    echo "Server Terminated"
}

?>
Это было полезно?

Решение

$_POST['submit'] has to be $_POST['server_stop']

<?php
if (isset($_POST['server_stop'])) {
    echo "Server Stopped";
}

if (isset($_POST['server_terminate'])) {
    echo "Server Terminated";
}

?>

Другие советы

Your code:

<?php
if (isset($_POST['server_stop'])) {
    echo "Server Stopped";
}

if (isset($_POST['server_terminate'])) {
    echo "Server Terminated"
}

?>

Both of these conditions are true on form submit. If you want to differentiate between which submit button was pressed, you'll need to make a hidden input and use some javascript:

<form method="post" name="server_information" id="server_information" action="test.php">
<input type="submit" name="server_stop" value="Stop Server" onclick="whatwasclicked(this);" />
<input type="submit" name="server_terminate" value="Terminate Server" onclick="whatwasclicked(this);" />
<input type="hidden" name="clicked" id="clicked" />
</form>

Your javascript will be:

function whatwasclicked(c) {
$("#clicked").val() = c.value;
}

Now on the PHP side, you'll check $_POST['clicked'] and see which button was pressed.

EDIT: Be aware there is Jquery being used here... so be sure to load that library.

If you don't want to use javascript at all, and you're only going to have those submit buttons in the form, you could also do this:

<form method="post" name="server_information" id="server_information" action="test.php">
<input type="submit" name="clicked" value="Stop Server"/>
</form>
<form method="post" name="server_information" id="server_information" action="test.php">
<input type="submit" name="clicked" value="Terminate Server"/>
</form>

Now just check for $_POST['clicked'] and you'll see which button was pressed.

if (isset($_POST['submit'])){
  $name =trim($_POST['name']);
  if (empty($name)){
    echo "please input name";    
  }
  else if (!preg_match("/^[a-zA-Z ]*$/",$name)){
    $namerr = "Only letters and white space allowed";
  }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top