如何捕获单选按钮值并将其发送到 PHP 纯文本文件? [英] How to capture radio button value and send it to a PHP plain text file?

查看:30
本文介绍了如何捕获单选按钮值并将其发送到 PHP 纯文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I looked up everywhere on Stackoverflow and other forums but couldn't find a solution to my problem. Here is the problem:

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

Create an HTML file that asks the user a question to vote on. There should be a few options done using radio buttons. There should also be a button to submit their vote. When they vote, they should be sent to a PHP file (different from the HTML file with the question) that records their vote and shows them the vote totals so far. To store the votes, you can just use a text file on the server. Part of the output is shown below.

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

I learned how to read and write things to the file. And for this, I am thinking I will have to read the text file every time since I have to keep track of each vote. But my problem is how can I send the individual values of radio buttons to the text file, because right now, what's happening is I can't select a radio button option with $_POST["first"] because you can select multiple radio buttons. And the only way to make it so that only one option can be selected is to give it the same name values to the radio buttons.

这是我目前的代码:HTML

Here is my code so far: 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;
    }
?>

先谢谢你!

推荐答案

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

You have to give all radio buttons, that share the same question, the same name. By doing this, it is not possible, to select multiple options anymore. To check what option has been selected, you can distinguish them by their values. Use this for 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'] 的值来检查已选择的选项:

In PHP you can check what option has been selected by checking the value of $_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";
    }

这篇关于如何捕获单选按钮值并将其发送到 PHP 纯文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆