我有一个包含数组名称的表单,每个名称都可以选择5,10,20和50的单选按钮。
我的代码包含:
$names = $names;
foreach ($names as $name)
{
// some div and html coding
"<input type='radio' name='".$name[0]."' value='5'>";
"<input type='radio' name='".$name[0]."' value='10'>";
"<input type='radio' name='".$name[0]."' value='20'>";
"<input type='radio' name='".$name[0]."' value='50'>";
// some other html and closing tags
}
页面的示例:
我的问题:
如何获取发布的值?
假设选择name2
和name4
选项10
并选择name5
选项20
,如何将已发布的值转换为类似于这样:
$result = array(
"name2" => "10",
"name4" => "10",
"name5" => "20"
);
非常感谢!
答案 0 :(得分:1)
而不是使用foreach()
你应该使用for循环
<?php
if(isset($_POST)){
$names=[1,2,3,4,5,6,7];
foreach ($names as $key => $value) {
if(in_array($_POST[$key], $names))
# code...
echo 'Do Your Logic Here ';
}
}
?>
<form action="s.php" method='post'>
<?php
for($i = 0; $i <= count($names) ; $i++){
echo "<input type='radio' name='".$names[$i]."' value='5'>";
echo "<input type='radio' name='".$names[$i]."' value='10'>";
echo "<input type='radio' name='".$names[$i]."' value='20'>";
echo "<input type='radio' name='".$names[$i]."' value='50'>";
}
echo "<input type='submit' />";
?>
</form>
这应该可以解决您的问题
答案 1 :(得分:-1)
try this..
<php $names = $names;?>
<form method="post">
<php
foreach ($names as $name)
{
// some div and html coding
"<input type='radio' name='name[".$name."]' value='5'>";
"<input type='radio' name='name[".$name."]' value='10'>";
"<input type='radio' name='name[".$name."]' value='20'>";
"<input type='radio' name='name[".$name."]'' value='50'>";
// some other html and closing tags
} ?>
<input type="submit" />
</form>