我想将我的 ajax 查询结果插入到文本框中。当用户选择产品代码时,单价应该显示在文本框中。输入数量时,必须将单价乘以数量并显示在文本框中。一旦文本框中的所有数据都可用,则需要将日期上传到 mysql db。这是我的代码。
<script type="text/javascript">
function showUP(str) {
if (str==""){
document.getElementById("UnitPrice").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("UnitPrice").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getunitprice.php?q="+str,true);
xmlhttp.send();
}
function multiply(Quantity) {
var totalPrice = parseFloat(document.getElementById("UnitPrice").innerHTML)*Quantity;
document.getElementById("TotalPrice").innerHTML = totalPrice;
}
</script>
</script>
<form action="addorderitemform.php" method="post" name="addorderitemform">
<table width="600px" border="0" cellspacing="1" cellpadding="3">
<tr>
<th width-"18%>OrderID:</th>
<td width="60%">
<select name="OrderID">
<option value="SelectCategory">Select a existing order</option>
<?php
$query = 'SELECT OrderID FROM customerorder';
$result = mysql_query ( $query );
while ( $row = mysql_fetch_assoc ( $result ) )
{
print "<option value=\"".$row['OrderID']."\">".$row['OrderID']."</option>\r";
}
?>
</select></td>
</tr>
<tr>
<th width-"18%>Product:</th>
<td width="60%">
<select name="ProductCode" id="ProductCode" onchange="showUP(this.value)">
<option value="SelectCategory">Select product</option>
<?php
$query1 = 'SELECT ProductCode, ProductName FROM Product';
$result1 = mysql_query ( $query1 );
while ( $row1 = mysql_fetch_assoc ( $result1 ) )
{
print "<option value=\"".$row1['ProductCode']."\">".$row1['ProductName']."</option>\r";
}
?>
</select></td>
<br/>
</tr>
<tr>
<th width-"18%>UnitPrice:</th>
<td width="60%">
<input type-"text" name="UnitPrice" id="UnitPrice" size="60" />
</td>
</tr>
<tr>
<th width-"18%>Quantity:</th>
<td width="60%">
<input type-"text" name="Quantity" id="Quantity" onblur= "multiply (this.value)" size="60" />
</td>
</tr>
<tr>
<th width-"18%>TotalPrice:</th>
<td width="60%">
<input type-"text" name="TotalPrice" id="TotalPrice" size="60" />
</td>
</tr>
</table>
<input type="submit" value="Add OrderItem" />
<input type="reset" value="Reset" />
</p>
</form>
GETUNITPRICE.PHP
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Order", $con);
$sql="SELECT CostPrice FROM Product WHERE ProductCode = '".$q."'";
$result2 = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
while($row2 = mysql_fetch_array($result2)) {
echo "".$row2['CostPrice']."";
}
mysql_close($con);
?>
然后将文本框中的详细信息上传到mysql数据库
<?php
$OrderID = (trim($_POST['OrderID']));
$ProductCode= (trim($_POST['ProductCode']));
$UnitPrice = (trim($_POST['UnitPrice']));
$Quantity = (trim($_POST['Quantity']));
$TotalPrice= (trim($_POST['TotalPrice']));
$host = "localhost";
$user = "root";
$password = "";
$db = "Order";
if (!$con = mysql_connect ($host, $user, $password))
{$message = "Server is not available. Please try again later";
echo "$message";
die ();
}
//or die ("Cannot connect to Server.");
mysql_select_db ($db) or die ("Database Order not available.");
$query = "INSERT INTO `OrderItem` (`OrderID`, `ProductCode`, `UnitPrice`, `Quantity`, `TotalPrice`) VALUES ('$OrderID','$ProductCode', '$UnitPrice', '$Quantity', '$TotalPrice')";
$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
//or die ("Insert into OrderItem failed.".mysql_error());
echo "<script> alert ('Your Information Was Successfully Saved')</script>";
header("Location: managesalesorder.php");
exit();
mysql_close($con);
?>