How to define form and submit button outside a foreach loop, and use arrays for a hidden variables

331 views Asked by At

I made a command button to take all the produse what a customer select(cart online) and send in the mysql db to record the command, but i face with a problem, when the customer sumbit the CONFIRM BUTTON (to give theirs adrress like a custom dates for the command) the script supposed to take their array items(products selected to buy) and give him a form to type their dates about the command. But is take me only the dates(prettotal produseID produseNume size) (only the dates for totalCOMANDApret and cantitate is showing me well) for the last array items select(last products in the cart) and I dont know how to figured out with this problem....

cart page

$i = 0; 
    foreach ($_SESSION["cart_array"] as $each_item) { 
        $item_id = $each_item['item_id'];
        // start select query stmt
        $stmt = $con->prepare('SELECT id, product_name, price, size, details FROM products WHERE id = ? LIMIT 1');
        $stmt->bind_param('i', $item_id);
        $stmt->execute();
        $stmt->bind_result($idSelect, $product_name, $price, $size, $details);//variabla pe care o vrei inlocuita prin bind_Result in loc de get_Result
        while ($stmt->fetch())    {
           $idSelect;
           $product_name; // faci acelasi lucru fara $row 
           $price;    
           $size;
           $details;    
        }
        $stmt->free_result();
        // stoop select stmt    
        $pricetotal = $price * $each_item['quantity'];
        $cartTotal = $pricetotal + $cartTotal;
                // Create the product array variable
        $product_id_array .= "$item_id-".$each_item['quantity'].","; 
        // Dynamic table row assembly
        $cartOutput .= "<tr>";
        $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="100" height="62" border="1" /></td>';
        $cartOutput .= '<td>' . $details . '</td>';
        $cartOutput .= '<td>' . $price . ' RON</td>';
        // How to split a query value into several mutiple rows for a select tag using explode() STAART SIZE
        $myArraySize = explode(' ',  $size);
        $cartOutput .= '<td><form action="cart.php" method="post"><select name="size">';
        foreach($myArraySize as $my_ArraySize)
        {
        $cartOutput .= '<option  value="'.$my_ArraySize.'">'.$my_ArraySize.'</option>';
        }
        $cartOutput .= '</select><input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />buc
        <input name="adjustBtn' . $item_id . '" type="submit" value="change" />
        <input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
        </form></td>';
        //STOP SIZE
        //$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
        $cartOutput .= '<td><center>' . $pricetotal . ' RON</center></td>';
        $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
        $cartOutput .= '</tr>';

cart page with form hidden value

$cofirmaComanda = '<form  action="cofirma_comanda.php" method="post">';
            foreach ($_SESSION["cart_array"] as $each_item) { 

            // ...
            $cofirmaComanda .='
            <input name="prettotal[]"  type="hidden" value="'. $pricetotal .'">
            <input name="produseID[]"  type="hidden" value="'. $item_id .'">
            <input name="produseNume[]"  type="hidden" value="'. $product_name .'">
            <input name="size[]"  type="hidden" value="'. $my_ArraySize .'">
            <input name="cantitate[]"  type="hidden" value="' . $each_item['quantity'] .'">
            <input name="produse[]"  type="hidden" value="'. $item_id .'">;
            <input name="totalCOMANDApret"  type="hidden" value="'. $cartTotal .'">';
                } 
            $cofirmaComanda .='<input type="submit" name="CofirmaComanda" value="cofirma_comandaTOW"></form>';  
        } //close the first loop

second page confirm_comanda.php with the get variable

for ($i=0; $i<count($_POST["prettotal"]); $i++) {
    $pretTotal = $_POST["prettotal"][$i];
    $PRODUSE = $_POST["produseID"][$i];
    $produseNume = $_POST["produseNume"][$i];
    $size = $_POST["size"][$i];
    $cantitate = $_POST["cantitate"][$i];
    $total = $_POST["totalCOMANDApret"][$i];
}

second form (on page confirm_comanda.php) to take customer dates for valid the command

    <form action=""   method="post">
<tr><td>nume: <input name="nume_client" type="text"></td></tr>

<tr><td>email: <input name="email_client" type="email"></td></tr>
<tr><td>telefon: <input name="telefon_client" type="number"></td></tr>
<tr><td>judet: <select name="judet_client">
<option value="Arges">Arges</option>
<option value="Arad">Arad</option>
<option value="Timis">Timis</option>
</select></td></tr>
<tr><td>adresa: <input name="adresa_client" type="text"></td></tr>
<input type="hidden" name="prettotal" value="<?php echo $_POST["prettotal"][$i]; ?>">
<input type="hidden" name="produseID" value="<?php echo $_POST['produseID'][$i]; ?>">
<input type="hidden" name="produseNume" value="<?php echo $_POST['produseNume'][$i]; ?>">
<input type="hidden" name="cantitate" value="<?php echo $_POST['cantitate'][$i]; ?>">
<input type="hidden" name="size" value="<?php echo $_POST['size'][$i]; ?>">
<tr><td><input  type="submit" name="sumbitDateClienti" value="Trimite Comanda"></td><td><input type="button" onClick="location.href='index.php';" value="Inapoi la Cumparaturi"></td></tr>
</form>

on the section is giving me error

Undefined offset: 2 in E:\wamp\www\MyOnlineStore\cofirma_comanda.php on line 85(means <input type="hidden" name="prettotal" value="<?php echo $_POST["prettotal"][$i]; ?>">)

1

There are 1 answers

0
Nana Partykar On

You are passing multiple values through cart.php page. And, accessing directly like <?php echo $_POST["prettotal"][$i]; ?>. So, obviously it will throw undefined offset error.

Update it like this way.

confirm_comanda.php

<form action=""   method="post">
  <tr><td>nume: <input name="nume_client" type="text"></td></tr>
  <tr><td>email: <input name="email_client" type="email"></td></tr>
  <tr><td>telefon: <input name="telefon_client" type="number"></td></tr>
  <tr>
    <td>judet: 
      <select name="judet_client">
        <option value="Arges">Arges</option>
        <option value="Arad">Arad</option>
        <option value="Timis">Timis</option>
      </select>
    </td>
  </tr>
  <tr><td>adresa: <input name="adresa_client" type="text"></td></tr>
  <?php
  for ($i=0; $i < count($_POST["prettotal"]); $i++) {?>
    <input type="hidden" name="prettotal[]" value="<?php echo $_POST["prettotal"][$i]; ?>">
    <input type="hidden" name="produseID[]" value="<?php echo $_POST['produseID'][$i]; ?>">
    <input type="hidden" name="produseNume[]" value="<?php echo $_POST['produseNume'][$i]; ?>">
    <input type="hidden" name="cantitate[]" value="<?php echo $_POST['cantitate'][$i]; ?>">
    <input type="hidden" name="size[]" value="<?php echo $_POST['size'][$i]; ?>">
  <?php }?>
  <tr>
    <td>
      <input type="submit" name="sumbitDateClienti" value="Trimite Comanda"></td><td>
      <input type="button" onClick="location.href='index.php';" value="Inapoi la Cumparaturi">
    </td>
  </tr>
</form>

I don't know exact logic behind what you are trying to do with those multiple values, but, there was the mistake which I found.