LLLLLLL Posted November 5, 2012 Share Posted November 5, 2012 This is a fairly typical piece of AJAX code... $.get( "someurl.php", { param1: someVariableValue, param2: 'someHardCodedString' }, function( data ) { On the back-end, PHP will receive $_GET with "param1" and "param2" keys, and the values as displayed above. My question is if it's possible to set the name of the keys param1 and param2 dynamically. I'm looking for a way to have a single function make the ajax calls, and to do that I won't have hard-coded "param1" keys, but the keys will be generated from whatever code I'm using to make the call. (Does that make sense?) Anyway, I am just looking for a way to set the keys of the GET (or POST) without hard-coding them on the page. Is this possible? Quote Link to comment Share on other sites More sharing options...
Jessica Posted November 5, 2012 Share Posted November 5, 2012 (edited) Use an array, that's all that is in Javascript, an array: JS: { param1: someVariableValue, param2: 'someHardCodedString' } php: array( 'param1'=>someVariableValue, 'param2'=>'someHardCodedString' ), Edited November 5, 2012 by Jessica Quote Link to comment Share on other sites More sharing options...
LLLLLLL Posted November 5, 2012 Author Share Posted November 5, 2012 Sorry, that's not really an answer that helps me. What does a function look like that creates this $.get call without hard-coded "param1" and "param2" key names? $.get( "someurl.php", { param1: someVariableValue, param2: 'someHardCodedString' }, function( data ) { Quote Link to comment Share on other sites More sharing options...
Jessica Posted November 5, 2012 Share Posted November 5, 2012 You're trying to make a function that generates that bit of Javascript...in PHP or JS? Quote Link to comment Share on other sites More sharing options...
LLLLLLL Posted November 5, 2012 Author Share Posted November 5, 2012 The function I've shown is on the client side. This is a sample generic function with param1 and param2 as the keys and specific values. I want to create a generic JS function to handle all my AJAX calls, and I want to pass that function a list of key/values in an array. But how does that make the $.get syntax look? Quote Link to comment Share on other sites More sharing options...
kicken Posted November 5, 2012 Share Posted November 5, 2012 You would just replace the second parameter of $.get with a variable. eg: var myobj = { param1: 'blah', param2: 'blah' }; $.get('someurl', myobj, function(data){ console.log(data); }); Then you just have to define what myobj is, which you can do conditionally if necessary. Quote Link to comment Share on other sites More sharing options...
LLLLLLL Posted November 5, 2012 Author Share Posted November 5, 2012 That's what I needed! Thanks. I didn't realize there was a second parameter option. Quote Link to comment Share on other sites More sharing options...
Jessica Posted November 5, 2012 Share Posted November 5, 2012 That's what I needed! Thanks. I didn't realize there was a second parameter option. That's what I told you in my first post, that array IS the second parameter. Quote Link to comment Share on other sites More sharing options...
LLLLLLL Posted November 5, 2012 Author Share Posted November 5, 2012 One response had the syntax as an example. One did not. Quote Link to comment Share on other sites More sharing options...
Jessica Posted November 5, 2012 Share Posted November 5, 2012 The function I've shown is on the client side. This is a sample generic function with param1 and param2 as the keys and specific values. And PHP can easily be used to generate Javascript. You were talking about PHP in the first post. I was simply trying to figure out what you were asking. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.