发送HTML中的段落值到PHP MySQL数据库

时间:2019-01-24 03:03:47

标签: php html

我创建了一个html页面,要求输入要购买的物品数量,然后将计算总数量。我可以将每个项目的数量值发送到数据库,因为它们是文本字段,但是我无法将段落(innerHTML)值发送到数据库。请问我该如何解决?

这是HTML代码。

 <body>

    <form id="Orders" method = "POST"/>
    Shirts ($1 Each) : 
   <input id="Shirts" type="text" name="Shirts" value=""/>

    Pants ($2 Each) :
<input id="Pants" type="text" name="Pants" value=""/>        

     Quantity:
 <p id="quantity" name ="quantity" value=""/></p>

     <input  type="submit" class="button" id="submit" value="Order"/>

<script>
    $('#submit').on('click',function(e){

   $.ajax({
           type: "POST",
           url: 'orders.php',
           data: $("#Orders").serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert(data); // show response from the php script.
           }
         });
});
</script>

这是JavaScript。

var Shirts = document.getElementById("Shirts").value;
var Pants = document.getElementById("Pants").value;

var Quantity = +Shirts + +Pants;
document.getElementById("quantity").innerHTML=Quantity;

这是php代码

<?php
$servername = "localhost";
$username = "Hello";
$password = "Test";
$dbname = "Test";


if(isset($_POST["Shirts"]) && isset($_POST["Pants"])  && isset($_POST    ["quantity"]))
{


    $shirts = $_POST["Shirts"];
$pants = $_POST["Pants"];
$quantity = $_POST["quantity"];


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO Test (Shirts, Pants, Quantity) VALUES ('$shirts','$pants','$quantity')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";

} else {
    echo "Failed " . $sql . "<br>" . $conn->error;
}

$conn->close();
}
?> 

0 个答案:

没有答案