Welcome to WebmasterWorld Guest from 50.16.112.199

Forum Moderators: open

Trouble building a dynamic data key

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

10+ Year Member



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)

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



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)

10+ Year Member



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

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



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

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month