-1

I can't find the misstake in the following code. I have a html Table with the fixed values item und price and calculated value total and the input value quantity

the calculation.js works fine as well as the connection to the database. it just cant find the array keys and tells me this if i click the button

Warning: Undefined array key "username" in C:\xampp\htdocs\Kulinarik\order_save.php on line 7
Warning: Undefined array key "item" in C:\xampp\htdocs\Kulinarik\order_save.php on line 8
Warning: Undefined array key "quantity" in C:\xampp\htdocs\Kulinarik\order_save.php on line 9

this is the code

html

                  <form method="POST" action="order_save.php">  
                <table class="u-table-entity" src="jquery.js">

                  <script scr="calculation.js"></script>
                  
                  <colgroup>
                    <col width="20%">
                    <col width="2.1%">
                    <col width="22%">
                    <col width="21.7%">
                    <col width="34.2%">
                  </colgroup>
                  <tbody class="u-table-alt-grey-5 u-table-body">
                    <tr style="height: 55px;">
                      <b>
                      <th class="u-table-cell u-table-cell-1"><b>Produkt</b><span style="font-weight: 700;"></span>
                      </th>
                      <th class="u-table-cell"></th>
                      <th class="u-table-cell u-table-cell-3"><b>Einzelpreis</b></th>
                      <th class="u-table-cell u-table-cell-4"><b>Menge</b></th>
                      <th class="u-table-cell u-table-cell-5"><b>Gesamtpreis</b></th>
                      </b>
                    </tr>
                    <tr style="height: 55px;">
                      <td class="u-table-cell">
                          <input type="text" class="item" value="Kornspitz" name="item" placeholder="Kornspitz" readonly />
                      </td>
                      <td class="u-table-cell"></td>
                      <td class="u-table-cell">
                          <input type="text" class="price" value="1.39" name="price" readonly/>
                      </td>
                      <td class="u-table-cell">
                        <input type="text" class="quantity" name="quantity"/> 
                      </td>
                      <td class="u-table-cell">
                        <input type="text" class="total" name="total" readonly/>
                      </td>
                    </tr>

                    <tr style="height: 55px;">
                      <td class="u-table-cell"></td>
                      <td class="u-table-cell"></td>
                      <td class="u-table-cell"></td>
                      <td class="u-table-cell u-table-cell-19"> Gesamtpreis:</td>
                      <td class="u-table-cell"></td>
                    </tr>
                    <tr style="height: 56px;">
                      <td class="u-table-cell"></td>
                      <td class="u-table-cell"></td>
                      <td class="u-table-cell"></td>
                      <td class="u-table-cell u-table-cell-24">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a class="u-active-none u-border-none u-btn u-button-link u-button-style u-hover-none u-none u-text-custom-color-1 u-text-hover-custom-color-2 u-btn-1" href="https://nicepage.com">AGB</a>
                      </td>
                      <td class="u-table-cell u-table-cell-25">
                        <a class="u-border-none u-btn u-button-style u-custom-color-1 u-hover-custom-color-2 u-btn-2" type="submit" href="order_save.php">BESTELLUNG</a>
                      </td>
                    </tr>
                  </tbody>
                </table>
              </form>

php

<?php
session_start();
?>
    <?php
    $con= mysqli_connect('localhost','root','123456');
    mysqli_select_db($con, 'orders');
    $username = $_SESSION['username'];
    $item = $_POST['item'];
    $quantity = $_POST['quantity'];
    ?>

the username i want to add is from the session_login maybe there is a way to add this too ? it is created with this code

<?php
    session_start();
?>
<?php
    $ldap_dn = "uid=".$_POST["username"].",dc=example,dc=com";
    $ldap_password = $_POST["password"];
    $ldap_con = ldap_connect("ldap.forumsys.com");
    ldap_set_option($ldap_con, LDAP_OPT_PROTOCOL_VERSION, 3);
    if(@ldap_bind($ldap_con,$ldap_dn,$ldap_password))
        {
        $_SESSION['username'] = $_POST["username"];
        header("Location: Startseite.php");
        }
    else
        {
        echo "Invalid Credential";
        }
?>

I would really appreciate your help thanks

Setzi138
  • 11
  • 4
  • If you have a `
    ` you have to have some form items, such as ``, `
    – Patrick Janser Oct 15 '21 at 08:35
  • 2
    `$_SESSION['username'] = $_POST["username"];` makes no sense because there's no `username` field in your form. If you want to get the username _from_ the session, then assign the session value to a variable, not the other way round! e.g. this probably makes more sense: `$username = $_SESSION["username"];` – ADyson Oct 15 '21 at 08:43
  • Anyway `BESTELLUNG` makes no sense. 1) a href link can only submit a GET, not a POST. 2) It's submitting it to a different location than the "action" of the form, so the code is inconsistent and unclear about what should happen, 3) `type="submit"` isn't a recognised attribute for an ` – ADyson Oct 15 '21 at 08:46
  • @ Patrick Janser There are Input fields in the table? @ADyson I understand the confusion I edited the code and enterd the whole script Thanks for your comments – Setzi138 Oct 15 '21 at 08:58
  • You'd still need to write `$username = $_SESSION['username'];` in the first script – ADyson Oct 15 '21 at 09:09

1 Answers1

0

I feel you should use the proper button for submitting the form, not a link. The link button is only redirecting you to the PHP page, while the form was not been submitted.

Your form should look like this:

              <form method="POST" action="order_save.php">  
            <table class="u-table-entity" src="jquery.js">

              <script scr="calculation.js"></script>
              
              <colgroup>
                <col width="20%">
                <col width="2.1%">
                <col width="22%">
                <col width="21.7%">
                <col width="34.2%">
              </colgroup>
              <tbody class="u-table-alt-grey-5 u-table-body">
                <tr style="height: 55px;">
                  <b>
                  <th class="u-table-cell u-table-cell-1"><b>Produkt</b><span style="font-weight: 700;"></span>
                  </th>
                  <th class="u-table-cell"></th>
                  <th class="u-table-cell u-table-cell-3"><b>Einzelpreis</b></th>
                  <th class="u-table-cell u-table-cell-4"><b>Menge</b></th>
                  <th class="u-table-cell u-table-cell-5"><b>Gesamtpreis</b></th>
                  </b>
                </tr>
                <tr style="height: 55px;">
                  <td class="u-table-cell">
                      <input type="text" class="item" value="Kornspitz" name="item" placeholder="Kornspitz" readonly />
                  </td>
                  <td class="u-table-cell"></td>
                  <td class="u-table-cell">
                      <input type="text" class="price" value="1.39" name="price" readonly/>
                  </td>
                  <td class="u-table-cell">
                    <input type="text" class="quantity" name="quantity"/> 
                  </td>
                  <td class="u-table-cell">
                    <input type="text" class="total" name="total" readonly/>
                  </td>
                </tr>

                <tr style="height: 55px;">
                  <td class="u-table-cell"></td>
                  <td class="u-table-cell"></td>
                  <td class="u-table-cell"></td>
                  <td class="u-table-cell u-table-cell-19"> Gesamtpreis:</td>
                  <td class="u-table-cell"></td>
                </tr>
                <tr style="height: 56px;">
                  <td class="u-table-cell"></td>
                  <td class="u-table-cell"></td>
                  <td class="u-table-cell"></td>
                  <td class="u-table-cell u-table-cell-24">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a class="u-active-none u-border-none u-btn u-button-link u-button-style u-hover-none u-none u-text-custom-color-1 u-text-hover-custom-color-2 u-btn-1" href="https://nicepage.com">AGB</a>
                  </td>
                  <td class="u-table-cell u-table-cell-25">
                    <button class="u-border-none u-btn u-button-style u-custom-color-1 u-hover-custom-color-2 u-btn-2" type="submit" value="BEST SELLING"></button>
                  </td>
                </tr>
              </tbody>
            </table>
          </form>

Moreover, assign a value to $username. This will not work

$_SESSION['username'] = $_POST["username"]

There is no input field in your form where a username is present. Fix that and that may get your program running

Sweta Jain
  • 3,248
  • 6
  • 30
  • 50