0

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

      }
    });

  });
});
Mohammad
  • 21,175
  • 15
  • 55
  • 84
Salil Lambay
  • 109
  • 1
  • 1
  • 6

2 Answers2

1

$.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']

shramee
  • 5,030
  • 1
  • 22
  • 46
  • Have you added `data: { language: SelectedLanguage },` to your `$.ajax` function params? – shramee Jul 24 '16 at 12:36
  • Try using `filter_input( INPUT_POST, 'language' )` to avoid error when not set... – shramee Jul 24 '16 at 12:44
  • that vanished the error. but the value is not getting sent to php page – Salil Lambay Jul 24 '16 at 12:53
  • @SalilLambay what do you see in Netwroks in Chrome DevTools? Are there "form data" and "language" in this request? – Иван Пшеницын Jul 24 '16 at 12:53
  • @ИванПшеницын data in networks cn be seen "language : en" but not able to still get data in print filter_input( INPUT_POST, 'language' ); or $_POST['language'] – Salil Lambay Jul 24 '16 at 13:17
  • @SalilLambay do you have any HTTP-redirects in this request? you can see this in Networks too. It looks like a second request. – Иван Пшеницын Jul 24 '16 at 13:23
  • @ИванПшеницын no... before making any select box change... i clear out network data.. then i change the select box value and only see this one request – Salil Lambay Jul 24 '16 at 13:28
  • @SalilLambay can you show your index.php file? Do you try to get $_POST['language'] in this file, right? – Иван Пшеницын Jul 24 '16 at 13:47
  • @ИванПшеницын i have uploaded the file at : https://www.dropbox.com/s/8ima72vfw0k3c1j/indexNew.php?dl=0 please check – Salil Lambay Jul 24 '16 at 13:59
  • @SalilLambay code is very simple and right. There is no place for error. The problem in the server ot PHP config, obviously. But I no have any recommndation, I'm sorry( You can check this thread, that is same problem: http://stackoverflow.com/questions/9914979/php-post-not-working – Иван Пшеницын Jul 24 '16 at 14:16
  • @wpdevelopment.me could you please check the file and suggest https://www.dropbox.com/s/8ima72vfw0k3c1j/indexNew.php?dl=0 – Salil Lambay Jul 24 '16 at 14:43
  • Hmm... what do you intend to do? Making ajax request to same page doesn't seem to make sense to me... – shramee Jul 24 '16 at 15:35
  • `language` var in your ajax success handler would contain entire html on the page is that what you desire...? – shramee Jul 24 '16 at 15:37
  • @ИванПшеницын finally got the solution. in usccess of ajax : these two lines --> var res = SelectedLanguage; $("#result").html(res); and now a div result in php will have the value assiging it to the php vairable – Salil Lambay Jul 24 '16 at 15:42
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){ 

              }
        });
MBA1990
  • 1
  • 2