将Ajax数据传递到PHP数组切换案例以处理SQL数据
I have 5 HTML input fields that each have to have their own lane weight displayed accordingly. I am not sure how I need to use Ajax here, but I need to be able to send to the PHP for each lane name and return those values back to their respective input fields in the HTML. All files are separate. So far I have it working if I hardcode the PHP variable "lane_name", but I need it to cycle through the lane names according to which input field is requiring it. For example,
<input name="Lead-1" />
needs to call the switch case "Lead-1" and display that weight value that's in the DB and so on and so forth... so I will need multiple statements withing my Ajax as well to display the right information as I have only the Lead-1 input field displaying info now.
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;
}
Actually, I think I just found an alternative way to go about this and it is working. I just returned an array in the PHP file and then am calling each array value from the 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);