1

I would like to send some data by using jquery ajax function to my PHP file.

I have created such function:

function ajax_call (url, select, select_name)
{

    $(select).change(function () {
          $(".result").fadeIn(400).html('<img src="ajax-loader.gif"/>');            
          var select_value = $(this).val();

            $.ajax({        
              type: 'POST', 
              url: url, 
              data: { select_name : select_value }, 
                  success: function(data){  
                      $(".result").html(data); 
                  } 
              });
    });
}

I call it:

ajax_call ('url path to my PHP file', '#my_select_div', 'my_data_name');

I have problem with this part:

data: { select_name : select_value }

I would like to get:

$_POST['my_data_name']

but I'm getting:

$_POST['select_name']

Any ideas?

Thanks for your answers.

1 Answer 1

1

When using object literal syntax, the key can be a string or an identifier. The identifier represents the key name, not a variable. You have to assign the key/value after creating the object if you want to use variable key names.

var data = {};
data[select_name] = select_value;
            $.ajax({        
              type: 'POST', 
              url: url, 
              data: data
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.