[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

JQuery post - how to use a variable to contain my post parameters

Posted on 2009-03-30
3
Medium Priority
?
1,761 Views
Last Modified: 2012-05-06
I'd like to use a function to "build" the parameters I want to send along a $.post

Something like this :

function makePostParm()
{
var t = '';
var parms = array("id","name");
var vals = array(100,"mark");
for(t=0;t<parms.length;t++){
   t = t + "," + parms[t] + ":" + vals[t];
}
}

function mypost(params)
{
   $.post("ajax.php", {params} );
}

var parameter = makePostParm();
mypost( parameter );

Is this at all possible or ??

Thx,
Mark
0
Comment
Question by:trg_dk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
alien109 earned 2000 total points
ID: 24019441
You should build an actual object, and not a string representation of it. You can do this through a function, or by explicitly setting them using json notation:

var params = {
    id:100,
    name:"mark"
}

$.post("ajax.php", params);

------
OR
------

function addParam(obj, key, value) {
    obj[key] = value;
}

var params = {};
addParam(params, "id", 100);
addParam(params, "name", "mark");

}

$.post("ajax.php", params);
0
 
LVL 12

Expert Comment

by:alien109
ID: 24019455
oops. left an extra "}" there at the end, right before the $.post call. That shouldn't be there!
0
 
LVL 2

Author Closing Comment

by:trg_dk
ID: 31564331
I made some minor changes - the function should return obj and the obj should be updated like this :

function addParams(obj,key,value)
{
obj[key] = value;
return obj;
}
var params = {};
params = addParams(params,"id",100);
etc.

At least that was what worked for me
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question