Skip to content Skip to sidebar Skip to footer

Create A Submit Form (wrap The Table Into A Form And Show It As A Table Again) From The Products That Were Ordered

EDIT: Maybe its easier to create a new column in the database. Named: to_order. Then create an UPDATE Query for each product . Something like : UPDATE products SET to_order = '[VA

Solution 1:

Web pages are stateless, in other words, each request is separate to the web server. Because of this there is no link between two requests, for example form1 and form2.

In order to overcome this, you must use some form of storage that will persist across two (or more separate requests). This is called persistent storage. For long term persistent storage, use databases. For short term, use PHP sessions. You can read up on PHP sessions in the PHP manual.

To store your values from form 1, save the values into session. In your form, you will point your form action to form1.php

form1_view.php:

<form action=<?php echo "form1.php" method="post">
   <input name="field1">
   <input type="form1_submit">
</form>

and your form input handler is form1.php

   if (isset($_POST['form1_submit']) {
      session_start();
      $_SESSION['form1_inputs'] = serialize($_POST);
   }

Then, when receiving the valued from form2, retrieve the values you stored from form1.

form2_view.php:

<form action=<?php echo "form2.php" method="post">
   <input name="field2">
   <input type="form2_submit">
</form>

Form2 is handled by its own handler. form2.php

   if (isset($_POST['form2_submit']) {
      session_start();
      $form1_values = unserialize($_SESSION['form1_inputs']);
      $form2_values = $_POST;

      // combine input from both forms into one variable.
      $all_form_values = array_merge($form1_values, $form2_values)

      // You can now save values from both forms. How you do this
      // ...will depend on how you save the values. This is an example.
      save_my_values($all_form_values); 
   }

Solution 2:

After hours trying I finally figured it out. I used $_SESSIONS to store data in. Thats one thing. Then, made sure that the inputs are all array.

while ($producten=mysqli_fetch_assoc($result_producten)) {
                                    echo "<form method='POST' action='verwerken.php'><tr>";
                                    echo "<td><input name='ids[]' type='text' readonly='true' value='".$producten['id']."' /></td>";
                                    echo "<td><input name='lev_id' type='text' readonly='true' value='".$producten['lev_id']."' /></td>";
                                    echo "<td><input name='producten[]' type='text' readonly='true' value='".$producten['productnaam']."' /></td>";


                                       echo "<td>
      <input tabindex='1' id='".$producten['minimum']."' name='".$producten['id']."' type='text' 
         oninput='calculateBestelling(this.value,this.name, this.id)' 
          onkeypress='return event.charCode >= 48 && event.charCode <= 57'/>
                  </td>";
                                    echo "<td><input id='".$producten['minimum']."'  type='text' readonly='true' value='".$producten['minimum']."' /></td>";

                                    echo "<td><input id='".$producten['id']."' name='test[]' type='text' readonly='true'  /></td>";


                                    echo "</tr>";

 }

This is the while for the table. Once you filled in all your STOCK values, it will automatically calculate the value to order. Thats working fine. Now when you click the submit button a next page will be openend. This page only inserts a query into a database. For every row it will update the to_order value in the database.

$order_test = $_SESSION['hoeveelheid'] =$_POST['test'];
    $ids_test = $_POST['ids'];
        $producten = $_SESSION['product'] = $_POST['producten'];

    foreach (array_combine($producten, $order_test) as $producten => $order_test) {

    echo 'Product: ' . $producten . ' - Te  bestellen: ' . $order_test.'<br>';
     mysqli_query($conn, "UPDATE producten SET bestellen = '".$order_test."' WHERE productnaam ='".$producten."'");

}

    $_SESSION['lev_id'] = $_POST['lev_id'];
    $_SESSION['check'] = 'true';
header('Location: bestelformulier.php');
exit();

        }

If the database is not that slow, you should not see this page. After the query is done, you will be redirected to the overview page. Bestelformulier.php.

Here you simply mysqli_fetch_assoc the values from the database again. And you have an updated form.

Thanks everyone for the help (:


Post a Comment for "Create A Submit Form (wrap The Table Into A Form And Show It As A Table Again) From The Products That Were Ordered"