似乎无法将多个带有jquery的变量传递给mysql [英] Can not seem to pass more than one variable with jquery to mysql

查看:59
本文介绍了似乎无法将多个带有jquery的变量传递给mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过几个例子,似乎无法理解使用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:

我有一个包含2个级联下拉菜单的页面((使用jquery在第一个下拉菜单的基础上更新第二个下拉菜单非常有用).

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.)

在选择第一个下拉jquery的更新第二个下拉并通过客户ID来创建在tblinvoice表的新纪录PHP脚本(这也是伟大工程没有任何问题.)

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.)

在第二个下拉选择,我需要带发票号码一起传递价值,我的PHP脚本,这样我就可以更新与instid备案.(这是部分不工作)

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)

如果我只传递instid并手动将发票号放在查询的where子句中,则一切正常.如果我省略where子句,那么所有记录都会按预期更新.我需要知道我做错了什么或缺少什么.
我将尝试在此处发布代码

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代码

$(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() {

                }
            });


        }
    });
});

我尝试使用以下数据:{inst_id:inst_id,custid:custid,invid:invid,}(不更新像这样的表)

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

我还尝试了以下数据:"inst_id =" + inst_id +& custid =" + custid +& invid =" + invid,(这也没有任何结果.)

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

有人可以看一下这个jquery,看看我是否犯了一个简单的错误?

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

推荐答案

尝试以下格式:

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

只要序列化JSON对象,然后让服务器知道数据类型,就可以将JSON对象发布到服务器.

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

首先,您需要定义JSON对象:

First you need to define your JSON object:

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

然后更新您的ajax以使用该对象的序列化版本,并让服务器知道数据类型:

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....

这篇关于似乎无法将多个带有jquery的变量传递给mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆