homepage Welcome to WebmasterWorld Guest from 184.73.52.98
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Trouble building a dynamic data key
neophyte




msg:4457219
 5:25 am on May 24, 2012 (gmt 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);
}
});
}

 

Fotiman




msg:4457378
 1:56 pm on May 24, 2012 (gmt 0)

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.

neophyte




msg:4457629
 1:07 am on May 25, 2012 (gmt 0)

Thanks very much Fotiman

Fotiman




msg:4457634
 1:16 am on May 25, 2012 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved