dsfds2343 2014-05-19 00:15
浏览 70

数组中的PhP单选按钮

I have a form where you can select a radio button and it should transfer what was selected to the next page. My problem is that no matter which radio button you choose it always transfers the value associated last radio button over instead of the one you chose.

So if I choose Around the World it carries 5 with it instead of 10

I am required to use the GET method.

Here is my code:

   $title = array("Around the World"=>"10","Coast to Coast"=>"7","The Big City"=>"5");

    foreach($title as $sub=>$s_value) {
        echo "$sub $$s_value";
        echo '<input type="radio" name="sub" value="', $sub,'">';
        echo "<br>";

    }

 if (empty($_GET["sub"])) {
} else {
    $sub = sub_input($_GET["sub"]);
}

if (empty($_GET["s_value"])) {
} else {
    $s_value = sub_input($_GET["s_value"]);
}

if (isset($title['sub'])){
    $valid=false;
}

This is the code for the next page:

echo "<b>$sub</b><br />";

echo "Base Total: $ $s_value/mon x $month months <br />";

Yes I have omitted a lot of things, because everything else in my code is fine.

I tried doing this as well, adding in an unset() statement but it didnt work. It completely deleted the value variable....

$title = array("Around the World"=>"10","Coast to Coast"=>"7","The Big City"=>"5");

    foreach($title as $sub=>$s_value) {
        echo "$sub $$s_value";
        echo '<input type="radio" name="sub" value="', $sub,'">';
        echo "<br>";

    unset($s_value);
    }

//I also tried putting the unset here//

 if (empty($_GET["sub"])) {
} else {
    $sub = sub_input($_GET["sub"]);
}

if (empty($_GET["s_value"])) {
} else {
    $s_value = sub_input($_GET["s_value"]);
}

if (isset($title['sub'])){
    $valid=false;
}
  • 写回答

2条回答 默认 最新

  • douqiu0351 2014-05-19 00:24
    关注

    You need to change the names of your variables $s & $s_value within the foreach loop. The foreach loop is setting these variables and they are then being accessed outside of the foreach loop if either of the GET values is empty such that there is no GET value to replace the contents of the variable. Therefore, it always uses 5 as the value because that is the last $s_value that you set.

    In summary, changing $s & $s_value within the foreach loop to something like $key & $value respectively will fix your problem with the array value. Alternatively, you could unset them after the foreach loop but before the if statements.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!