Php 动态表单提交
我有这样的表格Php 动态表单提交,php,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="subm
<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>";
}
$username=$\u POST[“username”];
foreach($\u POST['username']作为$ID=>$Value){
echo“值为“$s值”的复选框已选中!
”;
}
我只能得到一个输入字段,即用户名
我们能把所有3个输入都输入sub.php吗?我不知道你的问题是什么,但是你的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>
试试这个(不优雅,但应该告诉你哪里出了问题……)
$username=$\u POST[“username”];
foreach($\u POST['username']作为$ID=>$Value){
echo“值为“$value.”的复选框已选中!
”;
}
$homely=$_POST[“homely”];
foreach($\u POST['homely']作为$ht\u ID=>$ht\u值){
echo“带有值的复选框”。$ht_值。已选中!
”;
}
$username=$_POST[“国家];
foreach($\发布['country']作为$c\ ID=>$c\值){
echo“值为“$c_值”的复选框已选中!
”;
}
如果您的用户名、家乡、国家和顺序正确,那么您可以使用以下方法
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>";
}
foreach($\u POST['username']作为$ID=>$Value){
已选中echo“Username”。$Value.。
”;
echo“家乡”.$\u POST['Homeland'][$ID]。“已选中!
”;
echo“Country.”已选中“$\u POST['Country'][$ID]”!
”;
}
如果我理解这个问题
<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=$\u POST[“user”];
foreach($ID=>$info的用户){
echo“用户$ID({info['name']})居住在{$info['homely']}
”;
}
echo“所有用户名:”;
$all_id=数组_密钥($users);
foreach($all_id为$current_id){
echo$users[$current_id]['name']。“”;
}
是否要循环浏览所有已发布的值?只需在$\u POST
上执行foreach
。$\u POST['user']
来自哪里?为什么这些文本输入试图成为数组?代码中似乎有许多拼写错误或其他无法解释的东西。你到底想做什么?@David-Ya代码被编辑成用户名。谢谢你在var_dump($_POST)中得到了什么?@GBD现在,我只在sub.php中获得用户名值,但像wise一样,我需要在同一个foreach循环中输入所有输入,即用户名[]、家乡[]和国家[]。否,我对每个输入文本都有一个名称,如用户名[]、家乡[]、国家[]。HTML中还有其他错误,包括没有关闭标记。(并且没有关闭属性值,但这在原始问题中已经修复。)@David在我开始回答之前,这个问题一定已经修复了。这与我想要的逻辑是一样的,但是我们可以在一个foreach或while循环中完成所有事情,或者以任何其他方式获得所有三个输入吗?不,你至少需要两个foreach循环,一个用于完成帖子,第二步是获取每个post的key=>值,或者如果键总是相同的,那么您可以foreach(post as key=>value)并将已知的键定位在那里-这样它就是一个foreach循环
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>";
}
<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>
$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']." ";
}