0

我有这样的形式

 <form action="sub.php" method="post">
    <input type="text" name="username[]"><br>
    <input type="text" name="hometown[]"><br>
    <input type="text" name="country[]"><br>
    <input type="submit" value="submit">
</form>

子.php

$username = $_POST["username"];
foreach($_POST['username'] AS $ID => $Value){

        echo "Checkbox with value ".$sValue." was checked!<br>";
    }

我只能得到一个输入字段,即用户名 我们可以将所有 3 个输入都输入到 sub.php

4

4 回答 4

3

如果我明白这个问题

 <form action="sub.php" method="post">
    <input type="text" name="user[1][name]"><br>
    <input type="text" name="user[1][hometown]"><br>
    <input type="text" name="user[1][country]"><br>

    <input type="text" name="user[2][name]"><br>
    <input type="text" name="user[2][hometown]"><br>
    <input type="text" name="user[2][country]"><br>

    <input type="submit" value="submit">
</form>

PHP

$users = $_POST["user"];
foreach($users AS $ID => $info){
    echo "user $ID ({$info['name']}) lives in {$info['hometown']}<br>"; // dollar symbol added
}

echo "all usernames: ";
$all_ids = array_keys($users);
foreach($all_ids as $current_id) {
    echo $users[$current_id]['name']." ";
}
于 2012-10-09T17:39:38.007 回答
0

I'm not sure what your question is but there are a few issues with your html. It should be the following:

<form action="sub.php" method="post">
    <input type="text" name="username"><br>
    <input type="text" name="hometown"><br>
    <input type="text" name="country"><br>
    <input type="submit" value="submit>
</form>

I removed the brackets from the fields because brackets normally imply that you want your php code to see it as an array of values but you have single text fields.

If you want to get all of the inputs from the form you should use:

foreach($_POST AS $ID => $Value){
    echo "Textbox with value ". $Value ." was used!<br>";
}

I changed it to textbox because your form doesn't have any checkboxes

于 2012-10-09T17:06:16.780 回答
0

试试这个(不优雅,但应该告诉你哪里出错了..)

$username = $_POST["username"];
foreach($_POST['username'] AS $ID => $Value){

        echo "Checkbox with value ".$Value." was checked!<br>";
    }
$hometown = $_POST["hometown"];
foreach($_POST['hometown'] AS $ht_ID => $ht_Value){

        echo "Checkbox with value ".$ht_Value." was checked!<br>";
    }
$username = $_POST["country"];
foreach($_POST['country'] AS $c_ID => $c_Value){

        echo "Checkbox with value ".$c_Value." was checked!<br>";
    }
于 2012-10-09T17:29:26.890 回答
0

如果您的用户名,家乡,国家和顺序正确,那么您可以使用以下方式

   foreach($_POST['username'] AS $ID => $Value){
        echo "Username ".$Value." was checked!<br>";
        echo "Hometown ".$_POST['hometown'][$ID]." was checked!<br>";
        echo "Country ".$_POST['country'][$ID]." was checked!<br>";
    }
于 2012-10-09T17:36:35.197 回答