gpt4 book ai didi

php - 将 ajax 结果显示到文本框中

转载 作者:行者123 更新时间:2023-11-29 06:57:06 26 4
gpt4 key购买 nike

我想将我的 ajax 查询结果插入到文本框中。当用户选择产品代码时,单价应显示在文本框中。输入数量时,单价必须乘以数量并显示在文本框中。一旦文本框中的所有数据都可用,就需要将日期上传到 mysql 数据库中。这是我的代码。

<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);

?>

最佳答案

改变

document.getElementById("UnitPrice").innerHTML = xmlhttp.responseText;

document.getElementById("UnitPrice").value = xmlhttp.responseText;

关于php - 将 ajax 结果显示到文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12071810/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com