Welcome to WebmasterWorld Guest from 23.22.182.29

Forum Moderators: open

Message Too Old, No Replies

Trouble building a dynamic data key

     
5:25 am on May 24, 2012 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 20, 2004
posts: 615
votes: 0


Hello All -

I want to dynamically build the parameters for the data key of an ajax call but I can't get it to won't work.

I've check my results with alert() and everything looks perfect but it still won't work.

Is it the way I'm quoting the different elements in the datalist?

Can someone see where I'm going wrong?

++++++++++++++++++++++++++++++++++++++++++++

function ajaxAction(vars)
{
var id = vars[0];
var table = vars[1];
var category = vars[2];
var action = vars[3];

var dataList = x = null;

x = 'action: ' + action + ', ';
x += 'rowid: ' + id + ', ';
x += 'table: ' + table + ', ';
x += 'category: ' + category;

dataList = '{ ' + x + ' }';

alert(dataList);
// the alert shown is correct:
//{ action: Delete, rowid: 20, table: tbl_attack_log, category: Guestbook }

$.ajax({
type: "POST",
url: "assets/ajax/backend_action.php",
data: dataList,
dataType: "json",
success: function(data) {
afterAction(data);
}
});
}
1:56 pm on May 24, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4965
votes: 10


Anything that is string data, you would want to wrap in quotes. For example:
x = 'action: "' + action + '", ';
x += 'rowid: ' + id + ', ';
x += 'table: "' + table + '", ';
x += 'category: "' + category + '"';

Note, you may want to scrub those variables first to make sure none of them contain quotes.
1:07 am on May 25, 2012 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 20, 2004
posts: 615
votes: 0


Thanks very much Fotiman
1:16 am on May 25, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4965
votes: 10


To expand on what I said, you might want to use mysql_real_escape_string.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members