admin管理员组

文章数量:1332889

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);
        }
    });
}

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);
        }
    });
}
Share Improve this question edited Aug 8, 2015 at 21:49 Sverri M. Olsen 13.3k3 gold badges37 silver badges53 bronze badges asked Aug 8, 2015 at 21:45 Kerry DavisKerry Davis 1711 gold badge4 silver badges12 bronze badges 8
  • 1 hint type: "post" hint. – Script47 Commented Aug 8, 2015 at 21:47
  • You never use the field argument... why is it there? – Sverri M. Olsen Commented Aug 8, 2015 at 21:50
  • Is that it? just change GET to POST and it will work? – Kerry Davis Commented Aug 8, 2015 at 21:50
  • @SverriM.Olsen he uses it as the key for the mapstring variable. – Script47 Commented Aug 8, 2015 at 21:50
  • I do use the field argument. it is used as the dynamic parameter name in the data list. obviously it is being used wrong but that is the purpose of this question. To find out how to do it correctly. – Kerry Davis Commented Aug 8, 2015 at 21:51
 |  Show 3 more ments

3 Answers 3

Reset to default 4

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 ments 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.. i.e 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

本文标签: javascripthow to pass a variable parameter name using ajaxStack Overflow