[英]Form inside foreach loop php
I'm struggling with a problem. 我正在为一个问题而苦苦挣扎。
I have a foreach inside another foreach -that's not the problem- what happens is that, when I try to put a form inside the nested foreach, my controller doesn't react. 我在另一个foreach中有一个foreach,这不是问题,发生的事情是,当我尝试在嵌套的foreach中放置一个表单时,我的控制器没有反应。 When I click on the "+" button it's supposed to add the order and then redirect me to somewhere.php, otherwise, it should redirect me to nowhere.php.
当我单击“ +”按钮时,应该添加订单,然后将我重定向到某处。php,否则,它应该将我重定向至nowhere.php。 It just doesn't redirect me nowhere -without the .php, haha-, meaning that the
isset($_POST['do_add'}
is not working. 只是没有重定向我-没有.php,哈哈-,这意味着
isset($_POST['do_add'}
不起作用。
Any ideas? 有任何想法吗?
My controller: 我的控制器:
<?php
$br = new Brand;
$ord = new Order;
$temp = new Template('templates/menu.php');
$temp->br = $br->getAllBrands();
$temp->car = new Car;
echo $temp;
if(isset($_POST['do_add'])) {
$idPlate = $_POST['idCars'];
if ($ped->addCar($idCar)) {
redirect('somewhere.php');
} else {
redirect('nowhere.php');
}
}
?>
My template: 我的模板:
<table class = "table">
<?php foreach ($brands as $brand): ?>
<thead class = "thead-inverse">
<tr>
<th> <?php echo $brand->name; // these are just some car brands ?> </th>
</tr>
</thead>
<?php foreach ($cars->getCars($brand->idBrands) as $car): // here I get all the cars ordered by brands ?>
<tbody>
<form role = "form" method = "post" action = "carList.php">
<tr>
<td name = "idCar" value = "<?php echo $car->idCars; ?>"><p><?php echo $car->model; ?></p></td>
<td>
<button name = "do_add" type = "submit">+</button>
</td>
</tr>
</form>
</tbody>
<hr>
<?php endforeach; ?>
<?php endforeach; ?>
</table>
Thank you! 谢谢!
I think your problem is this line: 我认为您的问题是此行:
<td name = "idCar" value = "<?php echo $car->idCars; ?>"><p><?php echo $car->model; ?></p></td>
I never heard of submitting values of td tag directly with a form... did you mean something like this (create an input inside that td)? 我从未听说过直接使用表单提交td标签的值...您的意思是这样的吗(在td中创建输入)?
<td><input type="text" name="idCar" value="<?php echo $car->idCars; ?>"</td>
what gives you : 是什么让您:
var_dump($_POST['do_add'])
I think it's empty because you are submiting just the button with no value ! 我认为这是空的,因为您只提交了没有价值的按钮! Otherwise try to use :
否则请尝试使用:
trim($_POST['do_add'])
Best regards, 最好的祝福,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.