简体   繁体   中英

how to pass a variable parameter name using ajax

trying to invoke a backend php script which reads and writes to a mysql database and would like to define which field (aka column) I want to read or write from/to dynamically.

everything gets thru except the dynamic parameter name which I pass in as field to the javascript function.

if I hardcode field to 'mapstring' (which matches the column name in the mysql database), then it works. But writeabdata.php is written to write to any field name depending on what is passed into it.

What do I have to do to the field string parameter passed into writeabdata() so that it is passed correctly in the data portion of the .ajax call?

function writeabdata(table, id, field, mapstring) {
    //alert ("table = "+table+" id = "+id+" field = \'"+field+"\' value = "+value);
    $.ajax({
        type: 'GET',
        url: 'writeabdata.php',
        data: {
            'table': table,
            'id': id,
            field: mapstring
        },
        success: function (data) {
            alert ("data Saved "+ data);
        }
    });
}

Generally when writing you'll want to use the "POST" type rather than the "GET" type. POST is for POSTING data to the data store, and GET is for RETRIEVING it. Without some more code, though, its hard to debug this, so I'm going to take a couple of shots in the dark.

First off, clean up the code a bit and unify your format - put "field" in quotes like your other items. While this may not solve your problem, the JSON standard is actually defined as using DOUBLE QUOTES only.

Second off, if we could see the PHP code, that would help - my guess is that there's something wrong with how the response is interpreted. I suggest that for debug purposes you get Fiddler running and inspect the actual request to make sure that you're sending all required fields to the server.

Once you update us with more info, I can update my answer - but I'd start by switching to POST.

Update I think I misunderstood the question -- if you're looking to get data.field to really be data.somefield as in the name of that property could change to whatever you want, that's quite simple:

data[field] = mapstring

In other words:

function writeabdata(table, id, field, mapstring) {
    //alert ("table = "+table+" id = "+id+" field = \'"+field+"\' value = "+value);

    var dataObj = {
        'table': table,
        'id': id
    };
    dataObj[field] = mapstring;

    $.ajax({
        type: 'GET',
        url: 'writeabdata.php',
        data: dataObj,
        success: function (data) {
            alert ("data Saved "+ data);
        }
    });
}

Contrary to some of the comments you got, you can, as you see above, dynamically set property names using the array accessor on an object. And it works swimmingly. Build out your statically named data object properties, and then add the other ones via the array accessor.

You cannot set the field of an object literal (when you use {} directly in your code) using a variable.

For example:

var field = "b";
var myObject = {
    "a": "A",
    field: "B",
};

That object will look like this:

{
    a: "A",
    field: "B",
}

The reason this does not work is because fields are always considered to be strings. The fact that you do not need to put quotes around the field names is just language sugar, there to make it look better.

In order to create an object with a custom field you have to use the [] brackets, like this:

var field = "b";
var myObject = {
    "a": "A",
};
myObject[field] = "B";

Which will then work as intended:

{
    a: "A",
    b: "B",
}

Here in your function you are taking 4 arguments- table, id, field, mapstring.. and then making the field:mapstring.. ie you want the field value to be equal to mapstring than submit.. Why you want to take two arguments in a function to assign one's value to another... May be you just want field as 1 of the field of table.. So take 3 arguments- table, id, field..and assign the value of field similarly you assigned the values of table and id and see if it works...

Also replace GET with POST

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM