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 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab