我在这里尝试做的是我有 5 个输入字段,每个输入字段都有自己的 Lane-1、Lane-2、Poly-1、Poly-2、Poly-3 总权重,将由数据库中的内容显示...我可以使用 5 个不同的 JS 函数和 5 个单独的 php 文件以及与该车道类型专门相关的 SQL 语句来做到这一点,但如果可能的话,我宁愿在一个结构或一个文件和一个 JS 函数中完成这一切? 有什么想法可以解决这个问题吗?我让它只适用于 SQL 语句的“else”部分,但我需要获取与其他车道类型(lead-2、poly-1 等)有关的其他数据提前谢谢!
我什至不能说我做的这些都是对的,所以请随意责备我。
HTML
<body onload="getLaneWeight();">
<form id="Warehouse_Worksheet" name="Warehouse_Worksheet">
<table align="center">
<tbody>
<tr>
<th>
Lane Types
</th>
<th>
Total Weight
</th>
</tr>
<tr>
<td>
Lead 1
</td>
<td>
<input type="text" name="Lead-1" readonly="readonly" />
</td>
</tr>
<tr>
<td>
Lead 2
</td>
<td>
<input type="text" name="Lead-2" readonly="readonly" />
</td>
</tr>
<tr>
<td>
Poly 1
</td>
<td>
<input type="text" name="Poly-1" readonly="readonly" />
</td>
</tr>
<tr>
<td>
Poly 2
</td>
<td>
<input type="text" name="Poly-2" readonly="readonly" />
</td>
</tr>
<tr>
<td>
Poly 3
</td>
<td>
<input type="text" name="Poly-3" readonly="readonly" />
</td>
</tr>
</tbody>
</table>
</form>
</body>
JS文件
//Populate Lane Type Weight Table
function getLaneWeight() {
$.ajax({
type: 'GET',
url: './php/getLaneWeight.php',
data: 'lane_name=Lead-1',
success: function (mydata) {
document.forms['Warehouse_Worksheet'].elements['Lead-1'].value = mydata;
},
error: function () {
},
complete: function () {
getLead2();//Call next Lane Type
}
});
};
//Set Next Lane Weight
function getLead2() {
$.ajax({
type: 'GET',
url: './php/getLaneWeight.php',
data: 'lane_name=Lead-2',
success: function (mydata) {
document.forms['Warehouse_Worksheet'].elements['Lead-2'].value = mydata;
//alert(document.forms['Warehouse_Worksheet'].elements['Lead-1'].value);
},
error: function () {
},
complete: function () {
//getPoly1();//Call next Lane Type
}
});
};
PHP/SQL
if(isset ($_GET['Poly-2'])){
$sql="SELECT SUM(weight)
FROM bundle_lanes
WHERE lane_name = 'Poly-3'";
}else if(isset ($_GET['Poly-1'])){
$sql="SELECT SUM(weight)
FROM bundle_lanes
WHERE lane_name = 'Poly-2'";
}else if(isset ($_GET['Lead-2'])){
$sql="SELECT SUM(weight)
FROM bundle_lanes
WHERE lane_name = 'Poly-1'";
}else if(isset ($_GET['Lead-1'])){
$sql="SELECT SUM(weight)
FROM bundle_lanes
WHERE lane_name = 'Lead-2'";
}else{
$sql="SELECT SUM(weight)
FROM bundle_lanes
WHERE lane_name = 'Lead-1'";
}
$result = mysql_query($sql) or die ('Error'.mysql_error());
while ($data = mysql_fetch_row($result)) {
$weight = $data[0];
echo $weight;
}