Home > other >  Input checkboxes with different values/names
Input checkboxes with different values/names

Time:08-03

I have one page website with form: Image from my website

When user chose option 1, 2 or 3 and fill out form - then after pressing submit button, the data is entered into the database.

My database table structure:

ID | NAME | EMAIL | ADDRESS | CITY | ZIP | PRODUCT

There is no price row, because i cant figure out how to insert price value into checkbox. After user pressing submit button, all the data is inserted in the database, everything is fine, but I can't find a solution to add a price to each product in input checkbox field.

This is my query for insert data:

if (isset($_POST['submit'])) {

$stmt = $mysqli->prepare("INSERT INTO orders (name, address, email, zip, city, product) VALUES (?, ?, ?, ?, ?, ?)");

$name = $_POST['name'];
$address = $_POST['address'];
$email = $_POST['email'];
$zip = $_POST['zip'];
$city = $_POST['city'];
$product = $_POST['product'];

$stmt->bind_param("ssssss", $name, $address, $email, $zip, $city, $product);

$stmt->execute();

}

And this is my form:

<form action="" method="POST" >
              <div >
                <div >Wir liefern innerhalb von 24 Stunden. Mit 100% Geld Zurück Garantie
                </div>
                <div >Sofort lieferbar!
                </div>
                <div >
                    <input  type="text" name="name" placeholder="Vor-und Nachname" required="required" />
                    <input  type="text" name="email" placeholder="Email" required="required" />
                    <input  type="text" name="address" placeholder="Adresse" required="required" />
                    <input  type="text" name="zip" placeholder="Postleitzahl" required="required" />
                    <input  type="text" name="city" placeholder="Stadt" required="required" />
                </div>
                
<div >Produkt</div>
                
                <div >
                  <label ><input  type="radio" value="Product1" name="product1" checked="checked" /><span ></span><span >1 Flasche, 60 Pillen für 150 €</span>
                  </label>
                  <label ><input  type="radio" value="Product2" name="product2" /><span ></span><span >3 Flaschen, 180 Pillen für 300 €</span>
                  </label>
                  <label ><input  type="radio" value="Product3" name="product3" /><span ></span><span >6 Flaschen, 360 Pillen für 450 €</span>
                  </label>
                </div>
                
                <div >1 Monat nach der Erstlieferung erhalte ich monatlich im Abonnement 1 Flasche zu 149 €. Ich kann das Abonnement jederzeit kündigen.
                </div>

                <label >
                    <input type="checkbox"  />
                    <span ></span>
                    <span >Ja, meine Angaben sind richtig und vollständig, ich akzeptiere die <a href="#" target="_blank">allgemeinen Geschäftsbedingungen</a></span>
                </label>

              </div>
              <div >
                <div >Mit Klick auf „Kaufen“ bestellen Sie verbindlich.
                </div>
                <div >
                  <button  type="submit" name="submit"><span>Kaufen</span>
                  </button>
                  <div >Pillen werden in einer anonymen und subtil- Paket gesendet. (Pills passen in die Mailbox)
                  </div>
                </div>
              </div>
            </form>

My problem is that product 1, product 2 and product 3 - all 3 have their price. Product 1 - 150EUR, Product 2 - 300EUR, Product 3 - 450EUR. How can I add a price to the input (checkbox) fields? So I can create an invoice, because right now it only shows the product name, no price.

CodePudding user response:

According to your post:

Product 1 - 150EUR, Product 2 - 300EUR, Product 3 - 450EUR. How can I add a price to the input (checkbox) fields?

.. You need to change value in your radio buttons..

ALSO The name on your radio fields needs to be the same .. I changed it to product_check -- It can be seen on the php side as $_POST['product_check']

<label >
    <input  type="radio" value="Product 1 - 150EUR" name="product_check" checked="checked" />
    <span ></span>
    <span >1 Flasche, 60 Pillen für 150 €</span>
</label>
<label >
    <input  type="radio" value="Product 2 - 300EUR" name="product_check" />
    <span ></span>
    <span >3 Flaschen, 180 Pillen für 300 €</span>
</label>
<label >
    <input  type="radio" value="Product 3 - 450EUR" name="product_check" />
    <span ></span>
    <span >6 Flaschen, 360 Pillen für 450 €</span>
</label>

CodePudding user response:

There are several errors in your code:

1- name of input type radio is product1..2..3 While you receive the value of the post for product:

$product = $_POST['product'];

so undefined index: product;

2- How can I add a price to the input (checkbox) fields? as you said, Where is the fields you want to add price values to? It doesn't exist.

You need to assign a single price to the product, right?

So you need to create a table in the database and get product prices from it, then the html should be like this:

foreach ($resutls as result){echo'<input type="radio" name="product" value="'.$result->varofprice.'">';}

CodePudding user response:

Changed the name of the radio buttons so that the radio button deselects it's siblings when selecting itself, and added a simple jquery button to fetch the value of the currently selected one.

function showProductValue() {
  console.log("Product: "   $("input[name=product]:checked").val());
}
.check {
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label >
    <input  type="radio" value="150" name="product" checked="checked" />
    <span ></span>
    <span >1 Flasche, 60 Pillen für 150 €</span>
</label>
<label >
    <input  type="radio" value="300" name="product" />
    <span ></span>
    <span >3 Flaschen, 180 Pillen für 300 €</span>
</label>
<label >
    <input  type="radio" value="450" name="product" />
    <span ></span>
    <span >6 Flaschen, 360 Pillen für 450 €</span>
</label>
<button onClick="javascript:showProductValue();">Click Me</button>

  • Related