drkrsx3135168 2015-01-22 14:45
浏览 50
已采纳

似乎无法通过jquery将多个变量传递给mysql

I have seen several examples and can't seem to get the hang of passing more than one variable to mysql using jquery. Here is my situation:

I have a page with 2 cascading drop downs,( they work great using jquery to update second drop down based on the first drop down.)

when the first drop down is selected jquery updates the second drop down AND passes the customer id to a php script that creates a new record in the tblinvoice table (this also works great no problems.)

when the second drop down is selected I need to pass that value along with the invoice number to my php script so I can update the record with the instid.(this is the part that don't work)

If I only pass the instid and manually put the invoice number in the where clause of the query all works fine. If I omit the where clause all records are updated as expected. I need to know what I am doing wrong or what is missing.
I will try to post the code here

jquery code

$(document).ready(function() {

    $("select#cust").change(function() {
        var cust_id = $("select#cust option:selected").attr(
            'value');
        var test = $("#test").val();
        var din = $("#idate").val();
        $("#inst").html("");

        if (cust_id.length > 0) {
            $.ajax({
                type: "POST",
                url: "fetch_inst.php",
                data: "cust_id=" + cust_id,
                cache: false,
                beforeSend: function() {
                    $('#inst').html(
                        '<img src="loader.gif" alt="" width="24" height="24">'
                    );
                },
                success: function(html) {
                    $("#inst").html(html);
                }
            });

            if (test == 0) {
                $.ajax({
                    type: "POST",
                    url: "wo_start.php",
                    data: "cust_id=" + cust_id,
                    cache: false,
                    beforeSend: function() {

                    },
                    success: function(html) {
                        $("#invoice").html(html);
                        $("#test").val(1);
                        var inum = $("#inv").val();
                        $("#invnum").val(din +
                            "-" + inum);
                    }
                });
            }
        }
    });


    $("select#inst").change(function() {
        var inst_id = $("select#inst option:selected").attr(
            'value');
        var custid = $("select#cust option:selected").attr(
            'value');
        var invid = # ("#inv").val()
        if (inst_id.length > 0) {

            $.ajax({
                type: "POST",
                url: "wo_start.php",
                data: {
                    inst_id: inst_id,
                }
                cache: false,
                beforeSend: function() {

                },
                success: function() {

                }
            });


        }
    });
});

I have tried using data: {inst_id:inst_id,custid:custid,invid:invid,} (no update to the table like this)

I also tried data: "inst_id="+inst_id+"&custid="+custid+"&invid="+invid,(this also gives no results.)

Can someone PLEASE look at this jquery and see if I am making a simple error?

  • 写回答

1条回答 默认 最新

  • dtuct88226 2015-01-22 14:51
    关注

    Try this format:

    data: { inst_id: inst_id, custid: custid, invid: invid },
    

    You can post a JSON object to the server so long as you serialize it and then let the server know the data type.

    First you need to define your JSON object:

    var postData = { inst_id: inst_id, custid: custid, invid: invid };
    

    Then update your ajax to use the serialized version of that object and let the server know the data type:

    $.ajax({
                type: "POST",
                url: "fetch_inst.php",
                data: JSON.stringify(postData),
                contentType: "application/json",
                ..continue the rest of your ajax....
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛