【问题标题】:How to capture radio button value and send it to a PHP plain text file?如何捕获单选按钮值并将其发送到 PHP 纯文本文件?
【发布时间】:2016-06-24 20:45:34
【问题描述】:

所以我在 Stackoverflow 和其他论坛上到处查找,但找不到解决问题的方法。问题来了:

创建一个 HTML 文件,询问用户要投票的问题。应该有一些使用单选按钮完成的选项。还应该有一个按钮来提交他们的投票。当他们投票时,他们应该被发送到一个 PHP 文件(不同于带有问题的 HTML 文件),该文件记录他们的投票并向他们显示到目前为止的投票总数。要存储选票,您可以只使用服务器上的文本文件。部分输出如下所示。

我学会了如何读写文件。为此,我想我每次都必须阅读文本文件,因为我必须跟踪每次投票。但我的问题是如何将单选按钮的各个值发送到文本文件,因为现在,我无法使用 $_POST["first"] 选择单选按钮选项,因为您可以选择多个单选按钮.唯一可以选择的唯一方法是为单选按钮赋予相同的名称值。

到目前为止,这是我的代码: HTML

    </style>
  </head>
<body>
  <p>Whats your favorite class.</p>
  <form action="brianaCounted.php" method="post">
    <input type="radio" name="first" checked> 256
    <input type="radio" name="second"> 349
    <input type="radio" name="third"> 359
    <input type="submit" name="submit" value="submit">
  </form>
</body>
</html>

PHP

<?php
    if (isset($_POST['submit'])) {
        $first = 0;
        $second = 0;
        $third = 0;

        if (isset($_POST['first'])) {
            $first += 1;
        } else if (isset($_POST['first'])) {
            $second += 1;
        } else {
            $third += 1;
        }
        echo $first, $second, $third;
    }
?>

提前感谢您!

【问题讨论】:

    标签: php html server


    【解决方案1】:

    您必须提供所有具有相同问题、相同名称的单选按钮。通过这样做,不可能再选择多个选项。要检查选择了哪个选项,您可以通过它们的值来区分它们。将此用于 HTML:

    <body>
      <p>Whats your favorite class.</p>
      <form action="brianaCounted.php" method="post">
        <input type="radio" name="question1" value="option1" checked> 256
        <input type="radio" name="question1" value="option2"> 349
        <input type="radio" name="question1" value="option3"> 359
        <input type="submit" name="submit" value="submit">
      </form>
    </body>
    

    在 PHP 中,您可以通过检查 $_POST['question1'] 的值来检查选择了哪个选项:

    <?php
        if (isset($_POST['submit'])) {
            $first = 0;
            $second = 0;
            $third = 0;
    
            // Has question1 been answered?
            if (isset($_POST['question1'])) {
                $answer = $_POST['question1'];
    
                if ($answer == "option1") {
                    $first++;
                } else if ($answer == "option2") {
                    $second++;
                } else if ($answer == "option3") {
                    $third++;
                }
            }
    
            echo "$first, $second, $third";
        }
    

    【讨论】:

      【解决方案2】:

      您可以使用以下代码将数据保存到 JSON 文件中

       <form action="brianaCounted.php" method="post">
          <input type="radio" name="class" value="first" checked> 256
          <input type="radio" name="class" value="second"> 349
          <input type="radio" name="class" value="third"> 359
          <input type="submit" name="submit" value="submit">
        </form>
      

      PHP 代码

         <?php
      if (isset($_POST['submit'])) 
      {
      $userChoice = array();
      // read uses data from JSON file
      $userChoice = json_decode( file_get_contents('userChoice.json'),TRUE);
          if (isset($_POST['class']))
          {
               $value = $_POST['class'];
               $userChoice[$value]++;
          } 
          file_put_contents('userChoice.json',json_encode($userChoice));
      }
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-26
        • 1970-01-01
        • 2019-11-24
        • 1970-01-01
        • 2011-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多