在PHP中检索值有困难
我是PHP新手。我无法检索值在PHP中检索值有困难,php,Php,我是PHP新手。我无法检索值loopcount 以下是我编写的代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Order</ti
loopcount
以下是我编写的代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Order</title>
<script type="text/javascript">
//This function calculates the total bill by multiplying the price by quantity //selected
function order()
{
var x,y,i,j,p=0;
j=parseInt(document.getElementById('loopcount').value);
document.write(j);
for (i=0;i<=j;i++)
{
if(document.getElementById('itemCode[i]').checked==true){
x=parseFloat( document.getElementById('quantity[i]').value);
document.write(x);
y=parseFloat( document.getElementById('price[i]').value);
document.write(y);
p+=x*y;
}
}
var conf=confirm("The total amount is:"+ p);
if(conf==true){
header("Location:userHome.php");
}
else{
header("Location:orderveggies.php");
}
}
</script>
</head>
<body>
//the database has a table veggies.which contains the item code(primary key),vegetable name
//price/kilo and quantity in stock.The code bvelow displays data as checkboxes.select the
//items required and click on order .which gives the total bill ask if it can continue or
//change order
<form name="myForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="order()">
<table align="center" border="0">
<tr>
<th>item Code</th>
<th>Vegetable</th>
<th>Price/KG</th>
<th>Quantity</th>
</tr>
<?php
$db= mysqli_connect('localhost','root','','ourveggies')
or die("Error!Could not connect to the database");
$query="select itemCode,itemName,price from veggies";
$result=mysqli_query($db,$query)
or die("Error in query".mysqli_error($db));
$count=mysqli_affected_rows($db);
while($row= mysqli_fetch_array($result)){?>
<tr>
<td><input type="checkbox" name="itemCode[]" value="<?php echo $row['itemCode']; ?>"/><?php echo $row['itemCode']; ?></td>
<td><?php echo $row['itemName']; ?></td>
<td><input type="hidden" name="price[]" value="<?php echo $row['price']; ?>"/><?php echo $row['price']; ?></td>
<td><select name="quantity[]">
<option value="0.00">--Quantity--</option>
<option value="0.100">100 gm</option>
<option value="0.250">250 gm</option>
<option value="0.500">500 gm</option>
</select></td>
</tr>
<?php }?>
<input type="hidden" name="loopcount" value="<?php echo $count; ?>"/>
<tr>
<td><input type="submit" name="order" value="order" /></td>
</tr>
</table>
</form>
</body>
</html>
命令
//此函数通过将价格乘以所选数量//计算总账单
函数顺序()
{
变量x,y,i,j,p=0;
j=parseInt(document.getElementById('loopcount').value);
文件。编写(j);
for(i=0;i用于更新/删除查询,告诉您删除或更新了多少行(例如“受影响”)。您正在运行select查询,因此没有受影响的行-selects不更改数据
要获取select检索到的行数,请改为
您知道您试图在浏览器端javascript中使用服务器端php方法吗?它不起作用。javascript是否有问题,出于某种原因它没有检索loopcount值。首先,您正在执行getElementById()
,但没有id为loopcount的元素。
…名称不是id。现在检索到的是loopcount,而不是数组itemCode[],price[]。请仔细阅读文档。getElementById('price[i]').value);
,看看您是否能找出这是错误的原因。javascript应该如何知道[i]
在该字符串中实际上是一个数组引用,而i是一个变量?请尝试…getElementById('price['+i+']).value
。如果js以您在代码中期望的方式工作,那么i in price也会被转换为i变量的值,js中的任何字符串都不会受到随机变量的影响。真是一团糟。首先,您需要了解javascript和php之间的区别
var conf=confirm("The total amount is:"+ p);
if(conf==true){
header("Location:userHome.php");
}
else{
header("Location:orderveggies.php");
}
}