Salil Lambay Salil Lambay - 4 months ago 6
Ajax Question

Unable to send data to php on select box change

on a index.php page, inside html i have a selectbox

<select name="selectLanguage" id="selectLanguage" onchange="OnlanguageChange()">
<option value="">
Select your Language
</option>

<option value="en">
English
</option>

<option value="fr">
Fran├žais
</option>

<option value="de">
Deutsch
</option>

</select>


Wish to get the value of the select box onchange in my php code.

tried with ajax call :

$(document).ready(function(){
$("#selectLanguage").change(function(){
var SelectedLanguage = $(this).val();
alert(SelectedLanguage);
$.ajax({
type: "POST",
url: "index.php",
data: SelectedLanguage,
success: function(result){

}
});

});
});

Answer

$.ajax require data as an object...

$.ajax({
  type: "POST",
  url: "index.php",
  data: { language: SelectedLanguage },
  success: function(result) {

  }
});

In php access it as $_POST['language']