dsmvqp3124 2012-08-27 16:33
浏览 93
已采纳

在PHP中如何使用POST输出radiobox阵列的内容?

I want to output the values of the checkbox in the same way I'm outputting the values from the text fields. Since the checkbox can have multiple inputs I'm using an array for that but trying to cycle through the array for values hasn't worked for me. tried foreach($type as $item) and echoing $item within the HTML like it says in the PHP book I have but that hasn't worked. How should I do and where should the code be? I'm also unable to use PHP within the HTML for some reason, I'm not sure why that is or if its something to do with the echo<<<_END or not. Help appreciated.

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

?>

  • 写回答

3条回答 默认 最新

  • doujian3132 2012-08-27 17:00
    关注

    With the form as it is now, $_POST['type'] will be an array since it's using checkboxes (and named appropriately), not radios. Here I just implode it for display, but you can loop through it like any array. It should be worth noting that any time you're wondering what a form is giving you, you can var_dump($_POST) or var_dump($_GET) depending on where the data is coming from. It helps a lot with debugging.

    Here's what I got, I switched from heredoc, but your heredoc should work fine if you add $type back in somewhere, I didn't notice it in the original code:

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

    Addendum: If you switched and used radios like

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

    $_POST['type'] would be a simple string since you can only select one of the set.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置