具有名称字段变量的表单

时间:2014-08-03 08:53:57

标签: php variables post

我正在使用带有POST的表单。表单有一些可变的名称字段。

例如

for ($ca=0;$ca<=6;$ca++) {

后来

<input type=text name=ship_type[$ca] size=10 maxlength=10>
<input type=text name=ship_count[$ca] size=10 maxlength=10>
<input type=text name=from_fleet[$ca] size=10 maxlength=10>
<input type=text name=to_fleet[$ca] size=10 maxlength=10>

然后我用它来改变一些状态

    if (isset($_POST['do_transfer'])) {

// This fails to return a value
    $ship_type[$ca] = $_POST['ship_type'][".$ca."];
    echo $ship_type[$ca];

// This fails to return a value
    $ship_count[$ca] = $_POST['ship_count'][$ca];
    echo $ship_count[$ca];

// This fails to return a value
    $from_fleet[$ca] = $_POST['from_fleet'][$ca];
    echo $from_fleet[$ca];

// This fails to return a value
    $to_fleet[$ca] = $_POST['to_fleet'][$ca];
    echo $to_fleet[$ca];

// This returns the value
    $do_transfer = $_POST['do_transfer']; 
    echo $do_transfer; 
                                                     {  
    if (($from_fleet[$ca]!=$to_fleet[$ca]) && ($ShipTypes[$ship_type[$ca]]["Special"]!="PDS")) { 
        if (($fleets_in_use[$to_fleet[$ca]]!=1) && ($fleets_in_use[$from_fleet[$ca]]!=1)) { 
                // if its a full transfer

            if ($ship_type[$ca]=="all") { echo "test3";
                    //checking whats in from_fleet
                $query = "SELECT ammount, type FROM ships WHERE uid=".$user_data['id']." AND fleet=".$from_fleet[$ca]." AND eta=0";
                $result = mysql_query($query);

                    // setting fromfleet to 0
                $query = "UPDATE ships SET ammount=0 WHERE uid=".$user_data['id']." AND fleet=".$from_fleet[$ca]." AND eta=0 AND n_p='n'";
                mysql_query($query);

                while ($row = mysql_fetch_row($result)) {
                    $query = "UPDATE ships SET ammount=ammount+$row[0] WHERE uid=".$user_data['id']." AND fleet=".$to_fleet[$ca]." AND type=$row[1] AND eta=0";
                    mysql_query($query);
                }
                // reload the page so fleet details are displayed correctly
                header("Location: ?page=fleet");
            }
            else { echo "test4";
                if ($ship_count[$ca]>0) {
                    $query = "SELECT ammount FROM ships WHERE uid=".$user_data['id']." AND fleet=".$from_fleet[$ca]." AND type=".$ship_type[$ca]." AND eta=0";
                    $row = mysql_fetch_row(mysql_query($query));
                    $transfer_ammount = min($row[0], $ship_count[$ca]);
                    $query = "UPDATE ships SET ammount=ammount-$transfer_ammount WHERE uid=".$user_data['id']." AND fleet=".$from_fleet[$ca]." AND type=".$ship_type[$ca]." AND eta=0";
                    mysql_query($query);
                    $query = "UPDATE ships SET ammount=ammount+$transfer_ammount WHERE uid=".$user_data['id']." AND fleet=".$to_fleet[$ca]." AND type=".$ship_type[$ca]." AND eta=0";
                    mysql_query($query);
                    // reload the page so fleet details are displayed correctly
                    header("Location: ?page=fleet");
                }
            }
        }
    }
}

} }

我知道这个问题与以下内容有关

    $ship_type[$ca] = $_POST['ship_type'][$ca];
    $ship_count[$ca] = $_POST['ship_count'][$ca];
    $from_fleet[$ca] = $_POST['from_fleet'][$ca];
    $to_fleet[$ca] = $_POST['to_fleet'][$ca];

有没有人有解决方案?

2 个答案:

答案 0 :(得分:0)

你的 $ _ POST 数组就像

$_POST['ship_type'] = array($ca) //$ca  can be 0,1...6

您必须将其检索为

$shiptype = $_POST['ship_type'][0];

答案 1 :(得分:0)

不需要像这样写作

<input type=text name=ship_type[$ca] size=10 maxlength=10>

这是HTML代码,$ca是php代码,你写的就像

<input type=text name=ship_type[] size=10 maxlength=10>

因此,它将以数组形式发布。