在这里,我只想讨论以下内容:
我的 HTML 来自像颤动的代码:
<html>
<head>
<title>My Form</title>
</head>
<body>
<form id="sample" method="post" action="saveData.php">
Courses:
<input type="checkbox" name="check[]" Value="C++" />C++
<input type="checkbox" name="check[]" Value="PHP"/>PHP
<input type="checkbox" name="check[]" Value="MYSQL" />MYSQL
<input type="checkbox" name="check[]" Value=".Net"/>.Net
Gender:
<input type="radio" name="gen[]" Value="male"/>male
<input type="radio" name="gen[]" Value="female"/>Female
</form>
</body>
</html>
我想要如下所示的输出:
foreach ($_POST as $key => $val) {
$actVal .= "'".strtolower($key)."|".strtolower($val)."',";
$sqlin .= " ".strtolower($key)." VARCHAR(255) , ";
}
但是我得到了输出,就像哪个在那个选项中单击一样:
如下所示:
-----------------------------------------
male
C++
但我需要如下
:male,female
C++,PHP,MYSQL,.Net
如果您在
提交之前使用 javascript 将它们标记为选中,POST 将发送所有值。 $_POST["check"] 是一个数组。 使用 foreach 并从该数组中获取所有值。
当您循环访问将是一个数组的帖子数据时,我相信这就是为什么只返回其中一个元素的原因。
您可能想尝试这样的事情:
foreach ($_POST as $key => $val) {
if ($key == "check" || $key == "gen") { // If this is an array post field
foreach ($val as $val2) { // We need to loop through again since they're array post fields
$actVal .= "'" . strtolower($val2) . "'";
}
} else {
$actVal .= "'".strtolower($key)."|".strtolower($val)."',";
}
//$sqlin .= " ".strtolower($key)." VARCHAR(255) , "; // Worry about this separately, should be the same process
}
我想不出解决这个问题的方法。但还有另一种选择:
<input type="checkbox" name="check" value="php" />PHP
<input type="hidden" name="checklist" value="php" />
<input type="checkbox" name="check" value="MySQL" />MySQL
<input type="hidden" name="checklist" value="MySQL" />
这个想法是将复选框/单选按钮的所有值的列表存储在隐藏的输入中,以便在提交表单时在服务器端获取这些值的列表。
顺便说一句,你为什么需要它?