0

我有 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;
}
4

1 回答 1

0

实际上,我想我刚刚找到了另一种方法来解决这个问题并且它正在工作。我刚刚在 PHP 文件中返回了一个数组,然后从 AJax 调用每个数组值:

AJAX/JS

//Populate Lane Type Weight Table - JFLAY
function getLaneWeight() {
$.ajax({
    url: './php/getLaneWeight.php',
    dataType:'json',
    success: function (weight) {
        document.forms['Warehouse_Worksheet'].elements['Lead-1'].value = weight[0]['SUM(weight)'];
        document.forms['Warehouse_Worksheet'].elements['Lead-2'].value = weight[1]['SUM(weight)'];
        document.forms['Warehouse_Worksheet'].elements['Poly-1'].value = weight[2]['SUM(weight)'];
        document.forms['Warehouse_Worksheet'].elements['Poly-2'].value = weight[3]['SUM(weight)'];
        document.forms['Warehouse_Worksheet'].elements['Poly-3'].value = weight[4]['SUM(weight)'];
    },
    error: function () {

    }
});
};

PHP

$lane_name = array("Lead-1","Lead-2","Poly-1","Poly-2","Poly-3");

//Define JSON array
$array = array();

foreach ($lane_name as $value) {
    $sql="SELECT SUM(weight)
        FROM bundle_lanes
        WHERE lane_name = '$value'";

    $result = mysql_query($sql) or die ('Error'.mysql_error());

    while ($data = mysql_fetch_array($result)) {
        $array[] = $data;
    }
}

print json_encode($array);
于 2013-04-16T16:57:09.037 回答