【问题标题】:In PHP how to output the contents of a radiobox array using POST?在 PHP 中如何使用 POST 输出单选框数组的内容?
【发布时间】:2012-08-22 04:10:05
【问题描述】:

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

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

?>

【问题讨论】:

  • 不要在 heredoc 中包装你的 html。只需关闭 (?>) php 并将变量添加到您的 html 中,如下所示: ?>(closed php) =$title?>
  • @JurisMalinens 在需要大量变量的文件中观看和开发可能是一场噩梦

标签: php post radio


【解决方案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'] 将是一个简单的字符串,因为您只能选择其中一个。

【讨论】:

  • 谢谢,太好了,我在将复选框和单选按钮合并在一起的问题标题中犯了一个错误,我的意思是您的代码中的复选框。
  • 啊,很酷,很高兴来到这里。如果您需要对输入进行处理,PHP 提供的数组函数非常方便。
【解决方案2】:

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

$a=$_POST['type'];

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

【讨论】:

    【解决方案3】:

    好的,首先你不需要回显整个 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-11
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多