Solved

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

Posted on 2009-03-30
3
1,743 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 500 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
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…

749 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