[英]PHP Form submit to Get All Option Values?
在这里,我只想讨论以下内容:
我的HTML来自fllowing代码:
<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>
我希望OutPut像以下一样:
foreach ($_POST as $key => $val) {
$actVal .= "'".strtolower($key)."|".strtolower($val)."',";
$sqlin .= " ".strtolower($key)." VARCHAR(255) , ";
}
但是我得到了输出,就像点击了那个选项:
如下:
-----------------------------------------
male
C++
但我需要它像下面这样:
male,female
C++,PHP,MYSQL,.Net
当你循环遍历将成为数组的post数据时,我相信这就是为什么只返回其中一个元素的原因。
您可能想尝试这样的事情:
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" />
我们的想法是在隐藏的输入中存储复选框/单选按钮的所有值的列表,以便在提交表单时获取服务器端的那些值的列表。
顺便说一句,你为什么还需要呢?
如果您在提交前使用javascript将其标记为已选中,POST将发送所有值。 $ _POST [“check”]是一个数组。 使用foreach并从该数组中获取所有值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.