在PHP中循环处理单选按钮 [英] Processing radio buttons in loop in PHP

查看:138
本文介绍了在PHP中循环处理单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个迭代3次的循环。在循环内部,我有一个具有单选按钮的HTML表单。我正在使用PHP处理输入。当我回复表单数据时,它没有显示正确的值。这是处理数据的错误方式吗?任何帮助表示赞赏。

I have a loop that iterates 3 times. Inside the loop I have a HTML form that has radio buttons. I'm processing the input using PHP. When I echo the form data, its not showing the correct values. Is it a wrong way of processing the data ? Any help is appreciated.

test.php

test.php

<?php
for ($i = 1; $i <= 3; $i++) {
    ?>

    <form action = 'test.php' method = 'post'>
        <input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
        <input type="radio" name="num<?php echo $i; ?>" value="two">Two
    </form>

    <?php
}
?>

<input type = 'submit' value = 'Go'>

<?php
for ($i = 1; $i <= 3; $i++) {
    echo $_POST['num' . $i];
}
?>


推荐答案

将表单移到for循环之外。您目前正在创建三个表单,其中一个提交按钮(不附加到其中的任何一个)。

Move your form outside of your for loop. You are currently creating three forms with one submit button (which isn't attached to any of them).

尝试这种方式

<form action = 'test.php' method = 'post'>
<?php
for ($i = 1; $i <= 3; $i++) {
?>
    <input type="radio" name="num<?php echo $i; ?>" value="one">One<br>
    <input type="radio" name="num<?php echo $i; ?>" value="two">Two
<?php
}
?>

<input type = 'submit' value = 'Go'>
</form>

<?php
for ($i = 1; $i <= 3; $i++) {
    echo $_POST['num' . $i];
}
?>

这篇关于在PHP中循环处理单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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