无法在选择框更改时将数据发送到php

时间:2016-07-24 12:20:22

标签: javascript php jquery ajax

在index.php页面上,在html里面我有一个选择框

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

希望在我的php代码中获取选择框onchange的值。

尝试使用ajax调用:

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

      }
    });

  });
});

2 个答案:

答案 0 :(得分:1)

$.ajax需要数据作为对象......

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

  }
});

在php中将其访问为$_POST['language']

答案 1 :(得分:0)

           var formData = {     
              //get the value of language selected
               'SelectedLanguage':$("select[name='selectLanguage'] option:selected").val(),
            };
            $.ajax({ 
            type: "POST", 
            url: "index.php",
            //send language to index.php
            data: formData , 
            success: function(result){ 

              }
        });