0

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?

Pointy
  • 405,095
  • 59
  • 585
  • 614
phpnoobie
  • 61
  • 10
  • 2
    You're not "passing variables to mysql". You're passing values to a PHP script, which talks to mysql on your behalf. `var_dump($_POST)` would show exactly what's being received by PHP. – Marc B Jan 22 '15 at 14:52

1 Answers1

0

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....
Joe Hinton
  • 82
  • 5
  • I just tried that and now the jquery don't run also tried switching the quotes to the var1 var 2 but the same result jquery won't run – phpnoobie Jan 22 '15 at 15:00
  • I updated the string so you should be able to copy it right in. – Joe Hinton Jan 22 '15 at 15:06
  • Please edit your answer, and explain why this is a good solution. – Ben Jan 22 '15 at 15:22
  • @phpnoobie you have the comma in the wrong place in the version you just added to your edit. – Joe Hinton Jan 22 '15 at 15:29
  • @Ben I updated and tried to be more detailed and explain myself. – Joe Hinton Jan 22 '15 at 15:42
  • @JoeyHinton that did the trick. Man it is tough trying to learn 5 languages at once lol Thank you – phpnoobie Jan 22 '15 at 16:09
  • You'll get it eventually. I suggest if you don't understand what was happening there that you go research it now that you see how it works. Otherwise you'll get stuck again one day when you need to make a slight change. – Joe Hinton Jan 22 '15 at 16:11