Jump to content

Is There A Way To Parameterize The Parameters?


LLLLLLL

Recommended Posts

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?

Link to comment
Share on other sites

Use an array, that's all that is in Javascript, an array:

JS: 

{
  param1: someVariableValue,
  param2: 'someHardCodedString'
  }

 

php:

array(
  'param1'=>someVariableValue,
  'param2'=>'someHardCodedString'
),

Edited by Jessica
Link to comment
Share on other sites

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 ) {

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.