我尝试了很多方法将值从SPAN插入DB?span中显示的值来自Javascript。目前,当我运行时,UP和TP的0.00将显示javascript函数的值。当我插入MYSQL数据库时,除了UP和TP的值之外,我得到了所有其他插入的数据。我使用输入type="hidden"来传递值。这是我的密码。有人能帮忙吗?非常感谢。
<p></p><div><b>UnitPrice: <span id="UP">0.00</span> </b></div><p>
<p></p><div><b>TotalPrice: <span id="TP">0.00</span> </b></div><p>
<input type="hidden" name="UnitPrice" value="', document.getElementById("UP").value '"/>
<input type="hidden" name="TotalPrice" value="', document.getElementById("TP").value '"/>
$UnitPrice = (trim($_POST['UnitPrice']));
$TotalPrice= (trim($_POST['TotalPrice']));
$query = "INSERT INTO `OrderItem` (`UnitPrice`, `TotalPrice`) VALUES ('$UnitPrice','$TotalPrice')";
$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());
<script type="text/javascript">
function showUP(str) {
if (str==""){
document.getElementById("UP").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("UP").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getunitprice.php?q="+str,true);
xmlhttp.send();
}
function multiply(Quantity) {
var totalPrice = parseFloat(document.getElementById("UP").innerHTML)*Quantity;
document.getElementById("TP").innerHTML = totalPrice;
}
</script>
您要做的是javascript,但不能这样做。
当您获得<span id="UP">0.00</span>
时,您还可以添加具有该值的隐藏元素,将ID
添加到隐藏元素中,并修改您的javascript函数以将值添加到这些隐藏元素
<script type="text/javascript">
function showUP(str) {
if (str==""){
document.getElementById("UP").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("UP").innerHTML=xmlhttp.responseText;
document.getElementById("UnitPrice").value=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getunitprice.php?q="+str,true);
xmlhttp.send();
}
function multiply(Quantity) {
var totalPrice = parseFloat(document.getElementById("UP").innerHTML)*Quantity;
document.getElementById("TP").innerHTML = totalPrice;
document.getElementById("TotalPrice").value = totalPrice;
}
</script>
<p></p><div><b>UnitPrice: <span id="UP">0.00</span> </b><input type="hidden" name="UnitPrice" id="UnitPrice" value=""/></div><p>
<p></p><div><b>TotalPrice: <span id="TP">0.00</span> </b><input type="hidden" name="TotalPrice" id="TotalPrice" value=""/></div><p>