Multiple text box attached to current Objects array in php

I have list of products in database. I am loading products on page load and then displaying. I will ask user to enter the quantity for each product. Now after submitting the form, I want to retrieve all those quantities against each product. Below is my current code.

<?php
$productList;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    echo 'Product form submitted';
    foreach ($productList as $product) {
        // Code to retrieve quantity
    }
} else {
    include_once '../classes/OrderService.php';
    $orderService = new OrderService();
    $productList = $orderService->loadProducts();
}

?>

Below is html part.

    <?php foreach ($productList as $product) { ?>


        <b><a href="#" onclick="displayProductDetails('<?php echo $product->getProductName(); ?>');">
        <?php echo $product->getProductName();?></a></b>
        <br/>
        <?php echo $product->getDescription();?>
        <br/>
        <b>Price:</b> &nbsp;&nbsp;&nbsp;<?php echo $product->getPrice(); ?>
        <br/><br/>
        Qantity: <input type="text" style="padding-left: 5px;" name="<?php echo $product->getQuantity(); ?>" size="3" maxlength="3"/>

<?php } ?>

I have two problems here. First is when I submit the form and control goes to submit if block my product list is empty. Why it is empty if I have already populated while loading the page. Also how to get the quantity of every product entered by user.

Thanks in advance.

Answers


Subsequent requests after the first time you have built the page i.e. to submit the quantity of each product, will not automatically have the same data available on the form posting to the same script.

So you will need to perform an update query on a database table which is holding the products, then select them again. (presumably using the OrderService object which you already have built.

Here's a bit of help - although I don't have a whole lot of information about your data storage:

include_once '../classes/OrderService.php';
$orderService = new OrderService();
$productList = $orderService->loadProducts();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    foreach($productList as $product) {
        if(isset($_POST["quantity_" . $product->getId()])) {
            $product->updateQuantity($_POST["quantity_" . $product->getId());
        }
    }
}

apologies if any of the above produces an error. just typed it out quickly.

a quick note: on your input tag for quantity i'd go with something more descriptive for the name such as a product id or even quantity_<> so you can differentiate between the submissions on the server if you need to update multiple product quantities at once. it's also good practice to give those fields an id with the same value.

it is your "value" attribute which wants to display the actual quantity of products in a users order. e.g.

<input type="text" style="padding-left: 5px;" name="<?php echo $product->getId(); ?>" id="<?php echo $product->getId(); ?>" size="3" maxlength="3" value="<?php echo $product->getQuantity(); ?>"/>

Need Your Help

How to get the value inside a input name in html using php

php html

I would like to know how to get the value of name in input tag using php?

Formatting GDB template arguments in a backtrace

c++ templates gdb cgal

As I work with a C++ template library in GDB (through the terminal), I am trying to find a way of pretty-printing the template arguments when, for instance, viewing a backtrace.