1

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. 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. It just doesn't redirect me nowhere -without the .php, haha-, meaning that the isset($_POST['do_add'} is not working.

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!

5
  • Does the form is filling with the expected values? If it does, your problem is only when you try to retrieve de values in your controller. Try a var_dump($_POST), and see what you have there. Commented Jan 5, 2016 at 17:36
  • Having form tag directly after tbody is invalid. Browser rebuilds your invalid html and you have not what you expect. Commented Jan 5, 2016 at 17:36
  • Wait.. why am i not seeing any input in this form? It's just a submit button '-' Commented Jan 5, 2016 at 17:38
  • It's a table where I display the name of the car and a button. When I press the button it's supposed to submit the first td's name (idCar) with the value of the car's id. Commented Jan 5, 2016 at 17:47
  • u_mulder, and how do you think I can fix this? Commented Jan 5, 2016 at 18:03

2 Answers 2

1

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><input type="text" name="idCar" value="<?php echo $car->idCars; ?>"</td>
Sign up to request clarification or add additional context in comments.

Comments

0

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,

2 Comments

But I'm submiting -in the first <td> - the name 'idCar' with the value $car->idCars, don't I?
<td> is not an input element that can be submitted, so i don't think it works like this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.