1

我想以与从文本字段输出值相同的方式输出复选框的值。由于复选框可以有多个输入,我为此使用了一个数组,但尝试循环遍历数组以获取值对我来说不起作用。尝试了 foreach($type as $item) 并在 HTML 中回显 $item ,就像我在 PHP 书中所说的那样,但这并没有奏效。我应该怎么做,代码应该在哪里?由于某种原因,我也无法在 HTML 中使用 PHP,我不确定这是为什么,或者它是否与 echo<<<_END 有关。帮助表示赞赏。

<?php // formtest.php
if (isset($_POST['game'])) $game = $_POST['game'];
else $game = "(Not entered)";
if (isset($_POST['genre'])) $genre = $_POST['genre'];
else $genre = "(Not entered)";
if (isset($_POST['type'])) $type = $_POST['type'];
else $type = "(Not entered)";
echo <<<_END
<html>
<head>
    <title>Form Test</title>
</head>
<body>
Your game is: $game in the $genre genre and of the type<br />
<form method="post" action="formtest.php">
    What is your game?
    <input type="text" name="game" />
    <br />
    What is your genre?
    <input type="text" name="genre" />
    <br />
    Type?
       Retail <input type="checkbox" name="type[]" value="Retail" />
Downloadable <input type="checkbox" name="type[]" value="Downloadable" />
Free <input type="checkbox" name="type[]" value="Free" />
<br />
    <input type="submit" />
</form>
</body>
</html>
_END;

?>

4

3 回答 3

1

使用现在的形式,$_POST['type']将是一个数组,因为它使用复选框(并适当命名),而不是收音机。在这里,我只是将其内爆以进行显示,但您可以像任何数组一样循环遍历它。值得注意的是,任何时候你想知道一个表格给了你什么,你可以var_dump($_POST)或者var_dump($_GET)取决于数据来自哪里。它对调试有很大帮助。

这是我得到的,我从heredoc切换,但是如果你$type在某个地方添加回来,你的heredoc应该可以正常工作,我在原始代码中没有注意到它:

<?php // formtest.php
if (isset($_POST['game'])) $game = $_POST['game'];
else $game = "(Not entered)";
if (isset($_POST['genre'])) $genre = $_POST['genre']; //Edit: Fixed line, oops
else $genre = "(Not entered)";
if (isset($_POST['type'])) $type = implode(', ',$_POST['type']);
else $type = "(Not entered)";

//Normally I'd specify a charset, but for simplicity's sake I won't here.
$type = htmlspecialchars($type);
$game = htmlspecialchars($game);
$genre = htmlspecialchars($genre);

?>
<html>
<head>
    <title>Form Test</title>
</head>
<body>
Your game is: <?php echo $game; ?> in 
the <?php echo $genre; ?> genre and of the type <?php echo $type; ?><br />
<form method="post" action="">
    What is your game?
    <input type="text" name="game" />
    <br />
    What is your genre?
    <input type="text" name="genre" />
    <br />
    Type?
       Retail <input type="checkbox" name="type[]" value="Retail" />
Downloadable <input type="checkbox" name="type[]" value="Downloadable" />
Free <input type="checkbox" name="type[]" value="Free" />
<br />
    <input type="submit" />
</form>
</body>
</html>

附录:如果您切换并使用无线电,例如

<input type="radio" name="type" value="Downloadable" />

$_POST['type']将是一个简单的字符串,因为您只能选择其中一个。

于 2012-08-27T17:00:24.440 回答
0

好的,首先您不需要回显整个 html 输出。其次,您的问题是单选按钮,但 html 显示复选框。单选字段只会产生一个结果,因此您不需要在名称之后使用 []。当使用 [] 命名时,复选框将返回一个数组。因此,如果您使用复选框,则需要将结果作为数组处理。如果您将字段更改为无线电,它应该可以正常工作。

<?php // formtest.php
if (isset($_POST['game'])) {
    $game = $_POST['game'];
}
else { $game = "(Not entered)"; }
if (isset($_POST['genre'])) {
    $genre = $_POST['genre'];
}
else { $genre = "(Not entered)"; }
if (isset($_POST['type'])) {
    $type = $_POST['type'];
}
else { $type = "(Not entered)"; }
?>
<html>
  <head>
    <title>Form Test</title>
  </head>
  <body>
    Your game is: <?php echo $game; ?> in the <?php echo $genre; ?> genre and of the type <?php echo $type; ?><br />
    <form method="post" action="test.php">
      What is your game?
      <input type="text" name="game" <?php if ($game != "(Not entered)") { echo "value='" . $game . "'"; } ?> />
      <br />
      What is your genre?
      <input type="text" name="genre" <?php if ($genre != "(Not entered)") { echo "value='" . $genre . "'"; } ?> />
      <br />
      Type?
      Retail <input type="radio" name="type" value="Retail" <?php if ($type == "Retail") { echo "checked"; } ?> />
      Downloadable <input type="radio" name="type" value="Downloadable" <?php if ($type == "Downloadable") { echo "checked"; } ?> />
      Free <input type="radio" name="type" value="Free" <?php if ($type == "Free") { echo "checked"; } ?> />
      <br />
      <input type="submit" />
    </form>
  </body>
</html>
于 2012-08-27T16:50:37.053 回答
0

对于您发布的文件,type[] 将保存为数组。例如

$a=$_POST['type'];

尽管我没有发现对单选按钮执行此操作有任何意义,因为它们的目的是仅传递 1 个值(除非您特别想要)。

于 2012-08-27T16:59:44.510 回答