我有一个select
下拉框,它从Products
表中获取其值。此下拉框将所选产品的Product_ID
传递给insert
SQL语句。这些产品中的每一个都有价格(与产品表中的product_id
相同)。
如何以实时方式获取所选产品的价格(或者我认为动态是正确的词),以便我可以自动计算总金额?除了价格,我有一个数量和总平方英尺的文本框,这两个是用户手动输入。最后一个框(总金额)是我希望自动计算出现的值。
任何可以让我开始的帮助都将不胜感激!
无法发布图片,所以我很抱歉无法提供视觉效果.__。
编辑:我有一个半工作的jquery,我现在只需要获得所选产品的价格,这样我就可以把它换成var。
Editedit:添加了整个代码。
<?php
session_start();
if($_SESSION['LoginStatus'] == false)
header('location: ../index.php');
if(isset($_POST['logout']))
{
$_SESSION['LoginStatus'] = false;
header('location: ../index.php');
}
$conn = oci_connect("dbase", "dbase", "localhost/XE");
$query = "SELECT CUSTOMER_ID, CUSTOMER_FIRST_NAME, CUSTOMER_LAST_NAME FROM customers order by customer_id";
$query2 = "SELECT * FROM PRODUCTS ORDER BY PRODUCT_ID";
$query3 = "SELECT * FROM EMPLOYEES ORDER BY EMPLOYEE_ID";
$statement = oci_parse($conn, $query);
$statement2 = oci_parse($conn, $query2);
$statement3 = oci_parse($conn, $query3);
?>
<html>
<head>
<title>Add New Order Record</title>
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../css/stickyfooter.css" rel="stylesheet">
<script src="../js/jquery.js"></script>
<script>
$(function(){
$(#inp).keyup(function(){
var inpvalue= $('#inp').val();
$.ajax({
type: 'POST',
data: ({p : inpval}),
url: 'getprice.php',
success: function(data){
$('.results').html(data);
});
});
});
</script>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
</button>
<a class="navbar-brand">Top-C Graphics</a>
</div>
</div>
</nav><br><br><br>
<form action="addneworder2.php" name="frmAdd" method="post">
<table style="width: 500px" align="center" class="table table-hover">
<div align="center"><h5>Order</h5></div>
<tr>
<th>Customer*</th>
<td>
<select name="CustomerID" required>
<option value=""></option>
<?php
if(oci_execute($statement)){
while(ocifetchinto($statement, $row, OCI_ASSOC)){
printf("<option value='%s'>%s</option>\n", htmlentities($row['CUSTOMER_ID']),
htmlentities($row['CUSTOMER_ID'].' '.$row['CUSTOMER_FIRST_NAME'].' '.$row['CUSTOMER_LAST_NAME']));
}
}
?>
</select>
</td>
</tr>
<tr>
<th>Product Type*</th>
<td>
<select name="ProductID" required>
<option value=""></option>
<?php
if(oci_execute($statement2)){
while(ocifetchinto($statement2, $row, OCI_ASSOC)){
printf("<option value='%s'>%s</option>\n", htmlentities($row['PRODUCT_ID']),
htmlentities($row['PRODUCT_NAME'].' '.$row['PRODUCT_DESCRIPTION']));
}
}
?>
</select>
</td>
</tr>
<tr>
<th>Order Date* </th>
<td><input type="date" name="OrderDate" min="<?php echo date('Y-m-d');?>" required></td>
</tr>
<tr>
<th>Delivery Date* </th>
<td><input type="date" name="DeliveryDate" min="<?php echo date('Y-m-d');?>" required></td>
</tr>
<tr>
<th>Order Description* </th>
<td><textarea name="OrderDescription" required rows="5" cols="60" maxlength="500" placeholder="Enter description here."></textarea></td>
</tr>
<tr>
<th>Total Square Feet* </th>
<td><input type="text" name="TotalSF" id="TotalSF" required></td>
</tr>
<tr>
<th>Quantity* </th>
<td><input type="text" name="QtyPurchased" id="QtyPurchased" required></td>
</tr>
<tr>
<th>Total Amount* </th>
<td><span class="amount" id="TotalAmount"></span></td>
</tr>
<tr>
<th>Assigned Worker* </th>
<td>
<select name="Worker" required>
<option value=""></option>
<?php
if(oci_execute($statement3)){
while(ocifetchinto($statement3, $row, OCI_ASSOC)){
printf("<option value='%s'>%s</option>\n", htmlentities($row['EMPLOYEE_ID']),
htmlentities($row['EMPLOYEE_FIRST_NAME'].' '.$row['EMPLOYEE_LAST_NAME']));
}
}
?>
</select>
</td>
</tr>
<tr><td><b>*<font color="red">required fields</font></b></td></tr>
</table>
<div class="results">
</div>
<div align="center">
<br><input type="submit" name="submit" value="Add Record" onclick="return confirm('Add new order?')">
<input type="button" value="Go Back" onClick="history.go(-1);return true;">
</div>
</form>
</body>
</html>
<script>
var price = 0;
var quantity = 0;
var totalsf = 0;
$("#QtyPurchased").change( function(){
quantity = $("#QtyPurchased").val();
calcTotals();
});
$("#TotalSF").change( function() {
totalsf = $("#TotalSF").val();
calcTotals();
});
function calcTotals(){
$("#TotalAmount").text(quantity * totalsf);
}
</script>
执行插入的Php文件
<?php
session_start();
if($_SESSION['LoginStatus'] == false)
header('location: ../index.php');
if(isset($_POST['logout']))
{
$_SESSION['LoginStatus'] = false;
header('location: ../index.php');
}
?>
<html>
<head>
<title>Add New Order Record</title>
<link href="../css/bootstrap.min.css" rel="stylesheet">
<link href="../css/stickyfooter.css" rel="stylesheet">
</head>
<body>
<?php
$objConnect = oci_connect("dbase","dbase","localhost/XE");
$strSQL = "SELECT * FROM ORDERS WHERE Order_Number = (SELECT MAX(ORDER_NUMBER)+1 FROM ORDERS)";
$strSQL2 = "SELECT * FROM PAYMENTS WHERE Invoice_Number = '".$_POST['InvoiceNumber']."' ";
$strSQL3 = "SELECT PRICE FROM PRODUCTS WHERE PRODUCT_ID = '".$_POST['ProductID']."' ";
$objParse2 = oci_parse ($objConnect, $strSQL2);
oci_execute($objParse2, OCI_DEFAULT);
$objResult2 = oci_fetch_array($objParse2);
$objParse3 = oci_parse($objConnect, $strSQL3);
oci_execute($objParse3,OCI_DEFAULT);
$objResult3 = oci_fetch_array($objParse3,OCI_BOTH);
$price = $objResult3["PRICE"];
$objParse = oci_parse ($objConnect, $strSQL);
oci_execute($objParse,OCI_DEFAULT);
$objResult = oci_fetch_array($objParse);
if($objResult){
echo "Order Number already exists. Try again.";
echo "<br><input type='button' value='Go Back' onClick='history.go(-1);return true;'>";
echo "<br><a href='../orders.php'>Go back to Orders table.</a>";
}
else if($objResult2){
echo "Invoice Number already exists. Try again.";
echo "<br><input type='button' value='Go Back' onClick='history.go(-1);return true;'>";
echo "<br><a href='../orders.php'>Go back to Orders table.</a>";
}
else{
$qty = $_POST["QtyPurchased"];
$dimensions = $_POST["TotalSF"];
$TotalAmount = $qty * $dimensions * $price;
$strSQL = "";
$strSQL = "INSERT ALL ";
$strSQL .="INTO ORDERS ";
$strSQL .="(Order_Number, Customer_ID, Order_Description, Order_Date, Delivery_Date, Total_Amount) ";
$strSQL .="VALUES ";
$strSQL .="(orders_seq.nextval,'".$_POST["CustomerID"]."','".$_POST["OrderDescription"]."','".$_POST["OrderDate"]."','".$_POST["DeliveryDate"]."' ";
$strSQL .=",'".$TotalAmount."') ";
$strSQL .="INTO PAYMENTS ";
$strSQL .="(Order_Number, Invoice_Number, Date_Issued, Amount_Paid, Issued_By) ";
$strSQL .="VALUES ";
$strSQL .="(orders_seq.currval,'".$_POST["InvoiceNumber"]."','".$_POST["DateIssued"]."','".$_POST["InitialDeposit"]."','".$_POST["IssuedBy"]."') ";
$strSQL .="INTO LABOR ";
$strSQL .="(Employee_ID, Labor_Number, Date_Started, Date_Finished) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["Worker"]."',labor_seq.nextval,null,null) ";
$strSQL .="INTO ORDERLINE ";
$strSQL .="(Labor_Number, Qty_Purchased, Product_ID, Order_Number) ";
$strSQL .="VALUES ";
$strSQL .="(labor_seq.currval,'".$_POST["QtyPurchased"]."','".$_POST["ProductID"]."',orders_seq.currval) ";
$strSQL .="SELECT * from DUAL";
$objParse = oci_parse($objConnect, $strSQL);
$objExecute = oci_execute($objParse, OCI_DEFAULT);
if($objExecute){
oci_commit($objConnect); //*** Commit Transaction ***//
echo "Save completed.";
echo "<br><a href='../orders.php'>Go back to Orders table.</a>";
}
else{
oci_rollback($objConnect); //*** RollBack Transaction ***//
$e = oci_error($objParse);
echo "Error Save [".$e['message']."]";
echo "<br><input type='button' value='Go Back' onClick='history.go(-1);return true;'>";
}
}
oci_close($objConnect);
?>
</body>
</html>
getprice.php
<?php
$objConnect = oci_connect("dbase","dbase","localhost/XE");
$id=$_POST['p'];
$strSQL = "SELECT PRICE FROM PRODUCTS WHERE Product_ID = '".$id."' ";
while($row=oci_fetch_assoc($strSQL)){
$row['PRICE'];
}
?>
答案 0 :(得分:0)
请参考以下内容:如何以实时方式获取所选产品的价格(或者我认为这里的动态是正确的词),以便我可以自动计算总金额?
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1283&lngWId=5
对于上次查询: http://www.oracle.com/technetwork/database/database-technologies/rdb/automatic-columns-132042.pdf