I would like to insert my ajax query result into a textbox. When user select the productcode, the unitprice should be displayed in the textbox. and when the quantity is entered, the unitprice must be multiplied with the quantity and displayed in the textbox. once all data is available in textbox the date need to be uploaded into mysql db. Here is my code.
<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);
?>
And then upload the details in the textbox to mysql database
<?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);
?>
Change
to