动态表单提交

时间:2012-10-09 17:03:59

标签: php

我有像

这样的形式
 <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>

sub.php

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

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

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

4 个答案:

答案 0 :(得分:2)

如果我理解了这个问题

 <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>";
}

echo "all usernames: ";
$all_ids = array_keys($users);
foreach($all_ids as $current_id) {
    echo $users[$current_id]['name']." ";
}

答案 1 :(得分:0)

我不确定你的问题是什么,但你的html存在一些问题。它应该如下:

<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代码将其视为值数组,但您有单个文本字段。

如果您想从表格中获取所有输入,请使用:

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

我将其更改为文本框,因为您的表单没有任何复选框

答案 2 :(得分: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>";
    }

答案 3 :(得分: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>";
    }