目标:在 foreach 循环
中获取 $ _ POST 方法的按钮值$projects= 'Project1, Project2, Project3'//from mysql database
$projectNames = explode(',', $projects); // to separate the project names to display one by one on button.
显示按钮上的所有项目名称。
<?php foreach ($projectNames as $val):?>
<form action="projectSelected.php" method="post" id="project">
<button style="float:left" class="btn-default" value=""> <?php echo $val;?> </button>
问题陈述:当用户点击按钮&#39; Project1&#39;时,程序应该能够使用$_POST['projectSelected']
获取按钮的值。
帮助将不胜感激。
答案 0 :(得分:3)
将值设置为隐藏,然后发布值
<form action="projectSelected.php" method="post" id="project">
<input type="hidden" value="<?php echo $val ?>">
<input type="submit">
答案 1 :(得分:2)
1)更改变量的名称:
$Projects => $projects (PHP convention)
2)在爆炸功能后添加修剪
$projectNames = array_map('trim', $projectNames);
3)使用输入提交而不是按钮(similar question)
<input type="submit" style="float:left" class="btn-default" name="project" value="<?php echo $val ?>"/>
完整示例:
$projects = 'Project1, Project2, Project3'; //from mysql database
$projectNames = explode(',', $projects); // to separate the project names to display one by one on button
$projectNames = array_map('trim', $projectNames);
循环:
<form action="projectSelected.php" method="POST" id="project">
<?php foreach ($projectNames as $val) : ?>
<input type="submit" style="float:left" class="btn-default" name="project" value="<?php echo $val ?>"/>
<?php endforeach ?>
</form>
答案 2 :(得分:0)
做到这一点:
<button style="float:left" name = 'projectSelected' class="btn-default"
value=""> <?php echo $val;?> </button>
你设置按钮的名称将成为$_POST
数组