普通网友 2013-04-16 15:33
浏览 40
已采纳

将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;
}
  • 写回答

1条回答 默认 最新

  • dsxjot8620 2013-04-16 16:57
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题