我有 5 个 HTML 输入字段,每个字段都必须相应地显示自己的车道权重。我不确定我需要如何在这里使用 Ajax,但我需要能够将每个通道名称发送到 PHP,并将这些值返回到它们各自在 HTML 中的输入字段。所有文件都是分开的。到目前为止,如果我对 PHP 变量“lane_name”进行硬编码,它就可以工作,但是我需要它根据需要它的输入字段来循环遍历通道名称。例如,
<input name="Lead-1" />
需要调用switch case“Lead-1”并显示数据库中的权重值等等......所以我需要多个带有Ajax的语句来显示正确的信息,因为我只有Lead-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
function getLaneWeight() {
$.ajax({
type: 'GET',
url: './php/getLaneWeight.php',
data: 'Lead-1',
success: function (mydata) {
document.forms['Warehouse_Worksheet'].elements['Lead-1'].value = mydata;
},
error: function () {
},
complete: function () {
}
});
};
PHP
$lane_name = array("Lead-1","Lead-2","Poly-1","Poly-2","Poly-3");
switch ($lane_name[0])
{
case "Lead-1":
$sql="SELECT SUM(weight)
FROM bundle_lanes
WHERE lane_name = 'Lead-1'";
break;
case "Lead-2":
$sql="SELECT SUM(weight)
FROM bundle_lanes
WHERE lane_name = 'Lead-2'";
break;
case "Poly-1":
$sql="SELECT SUM(weight)
FROM bundle_lanes
WHERE lane_name = 'Poly-1'";
break;
case "Poly-2":
$sql="SELECT SUM(weight)
FROM bundle_lanes
WHERE lane_name = 'Poly-2'";
break;
case "Poly-3":
$sql="SELECT SUM(weight)
FROM bundle_lanes
WHERE lane_name = 'Poly-3'";
break;
}
$result = mysql_query($sql) or die ('Error'.mysql_error());
while ($data = mysql_fetch_row($result)) {
$weight = $data[0];
echo $weight;
}